Page Template as Custom Post Type Archive

Since WordPress version 4.4 the hook ‘theme_page_templates’ allows to set arbitrary page templates. It means that it’s possible to show arbitrary values in the “Page Template” menu of the page edit screen, and when selected, the value will be stored in the page template meta for the page. This means that you can “automagically” create … Read more

What are the differences between custom post type and custom page templates?

Taxonomies are categorizations of data. Tags are a taxonomy. Categories are another taxonomy. If you are building a movie website and want to show which actors starred in which movie, “Movies” would be a custom taxonomy. Custom Post Types are really just custom data items. They’re stored just like posts and pages, but aren’t considered … Read more

Add custom template page programmatically

The article linked is on the right track, but i’ll make it more simple for you.. 😉 add_filter( ‘page_template’, ‘catch_plugin_template’ ); function catch_plugin_template( $template ) { if( ‘tp-file.php’ == basename( $template ) ) $template = WP_PLUGIN_DIR . ‘/yourpluginname/tp-file.php’; return $template; } The filter basically looks to see if your special page template is set for … Read more

Passing parameters to a custom page template using clean urls

add_rewrite_rule() allows you to turn the pretty url into variables. numbers: (\d*) section: /rid/ or /pageid/ slug: ([a-zA-Z0-9-]+ Here is a class to register the rewrite and handle the request if a match has been found. <?php if ( ! class_exists( ‘CPTURLRewrite’ ) ): class CPTURLRewrite { const ENDPOINT_QUERY_NAME = ‘pageid’; const ENDPOINT_QUERY_PARAM = ‘__pageid’; … Read more

How to retrieve $_GET variables from rewritten URLs?

To be added on init: To register your custom variable (‘id’ in the question) add_rewrite_tag(‘%mycustomvar%’,'([^&]+)’); To create a re-write rule: add_rewrite_rule(‘^product/([0-9]{1,})/?’,’index.php?p=4&mycustomvar=$matches[1]’,’top’) 4 is the id of the ‘product’ page. You will need to flush rewrite rules once after adding these (go to Permalink settings page) You can get the value of mycustomvar: get_query_var( ‘mycustomvar’ ). … Read more

Define page template in wp_insert_post

From the wp_insert_post() documentation, the page_template argument reads as follow: page_template: If post_type is ‘page’, will attempt to set the page template. On failure, the function will return either a WP_Error or 0, and stop before the final actions are called. If the post_type is not ‘page’, the parameter is ignored. You can set the … Read more