5

I tried

add_filter( 'page_template', 'single_page_template' );

function single_page_template( $template )
  {
      if ( is_singular( 'dwqa-question' ) ) {

          $template =  get_stylesheet_directory() . '/' . page-question.php;
      }
      return $template;
  }

now, if I will out put echo get_page_template(); I am getting

/var/www/html/my-site/wp-content/themes/theme-child/page-question.php

but modifying the page-question.php file not effecting the output ,adding some text to this template like

get_header(); ?>zzzzzzzzz

not getting out put. page.php is assign to single question page using template_include hook is this because of that , but if yes echoing get_page_template() should also give page.php tempalte path as output .

Just modified page.php like

get_header(); ?>aaaa

and aaaa is appearing on page .

How I should override it ?? please let me know if I should provide any other detail ?

6
  • 1
    Can you provide more details? such as the custom post type slug, is dwqa-question the post type slug in question? if so you can have a custom template specific to this post type by having single-posttypeslug.php in your theme folder Commented Sep 20, 2016 at 16:31
  • @AhmedElmahdy creating a single-dwqa-question.php will not work as the plugin has used template_include hook to force it use page.php and I am trying to override that . Commented Sep 20, 2016 at 16:40
  • Maybe they're using a hook with higher priority that's why it overrides yours? Commented Sep 20, 2016 at 16:46
  • @AhmedElmahdy but get_page_template() function is displaying my template as I have mentioned in question . Commented Sep 20, 2016 at 17:13
  • 1
    I think this is more of a question for the DW Question & Answer forum than here, because you're asking about overriding that plugin's particular implementation of determining the template to use, and not about the standard WordPress implementation. Commented Sep 20, 2016 at 21:05

4 Answers 4

7

From your question, it seems you're trying to override the single template of a custom post type. If so, the filter you want to use is single_template and not page_template:

function single_page_template($single_template) {
    global $post;

    if ($post->post_type == 'dwqa-question') {
        $single_template = get_stylesheet_directory() . '/page-question.php';
    }

    return $single_template;
}
add_filter( 'single_template', 'single_page_template' );

I once had this issue and it was driving me crazy, until I found the filter on the WordPress Codex: https://codex.wordpress.org/Plugin_API/Filter_Reference/single_template

1
  • 1
    Hi, '/' . page-question.php will most probably crash your website.
    – leaf
    Commented Dec 12, 2020 at 5:53
1

According to the Theme Handbook (https://developer.wordpress.org/themes/template-files-section/page-template-files/page-templates/#creating-custom-page-templates-for-global-use), "Do not use page- as a prefix, as WordPress will interpret the file as a specialized template, meant to apply to only one page on your site."

So - try renaming page-question.php something like tpl-question.php (tpl is a common prefix, short for template).

3
  • Thank you for your effort, but it does not solved the problem facing same situation. Commented Sep 20, 2016 at 16:09
  • You could try giving your function a higher priority - add_filter( 'page_template', 'single_page_template', 100 );
    – WebElaine
    Commented Sep 20, 2016 at 21:10
  • sorry friend, but I have already tried even 9999 and it did not worked . Commented Sep 21, 2016 at 6:06
1

You mispelled the "page-question.php" portion of code, it must be included in quotes:

$template =  get_stylesheet_directory() . '/page-question.php';
0

Just try to implement flush of the rewrite rules function.

    flush_rewrite_rules();

You are free to use the alternative way,

    global $wp_rewrite;
    $wp_rewrite->flush_rules( true );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.