tommcfarlin/page-template-example

No working with WordPress v4.7

Opened this issue ยท 5 comments

Hello Tom,

Unfortunatelly this approach will no longer work with the upcoming WordPress v4.7

Any ideas on how we can make it work so that we can continue using the code?

Many thanks,
Zulf

Thanks for the heads up on this. I'll try and take a look at it as soon as I get a chance (before the release of 4.7).

Hey guys,
After doing some research with the changes in templates for 4.7, if you change lines 81 -> 82 like below...

    if ( version_compare( floatval($GLOBALS['wp_version']), '4.7', '<' ) ) { // 4.6 and older
            add_filter(
                'page_attributes_dropdown_pages_args',
                array( $this, 'register_project_templates' )
            );
      } else { // Add a filter to the wp 4.7 version attributes metabox
            add_filter(
                'theme_page_templates', array( $this, 'add_new_template' )
            );
      }

Then add the simple function to use for version 4.7 up...

    /**
     * Adds our template to the page dropdown for v4.7+
     *
     **/
    public function add_new_template( $posts_templates ) {
        $posts_templates = array_merge( $posts_templates, $this->templates );
        return $posts_templates;
    }

It works for me, here's the page I referenced for some more info on the changes https://make.wordpress.org/core/2016/11/03/post-type-templates-in-4-7/
It probably could use some more testing but it works for my purposes.

This works for me as well.

I'm looking to have this plugin re-written hopefully sooner rather than later so it will include support for 4.7 and will be much better organized from an OOP standpoint.

Thanks for the comments thus far :).

The solution suggested by @leeb003 works perfectly for me. Thank you!
I'd be very interested in an updated version of this plugin.