1

Pretty much every guide ive come across for adding php pages to Wordpress involves adding it at the theme level. e.g. How to add a PHP page to WordPress?. I want to add a new public facing page to multiple sites via a plugin. I dont want to have to add this to dozens of themes. If i can add it ad the plugin level it will work for all sites.

I have this working, but i need some way of injecting this into a theme. In order to get the sidebar and stuff without having to add custom css for each theme.

Ive started by adding a rewrite rule

RewriteRule ^mypage /wp-content/plugins/myplugin/mypage.php [QSA,L]

Then page.php contains

require_once('../../../wp-blog-header.php');
get_header();

//custom php content
//get_sidebar(); // how to get this working.
get_footer();

This also works, but the problem im having is with sidebars. Some themes dont have them and others do. Not all sidebars are 30% etc. I dont know how to build the div structure here to make it work. Some themes are 100% page width, but this looks ugly when viewed on other themes that are a fixed width. I have been able to come up with some compromises, but id rather be able to do this right.

In summery my main question here is. Is it possible to call a method that will inject html into a theme page. e.g. generate_page($html);. This method will then go to page.php of the theme and inject $html into the content area of the theme.

Edit In an attempt to dynamically inject content into an unknown theme ive done the following

global $post;

$post->post_content = "TEST PAGE content";
$post->post_title = "Page Title";
$post->post_name = "test-page";

include get_template_directory()."/page.php";

This works for some themes and not others. Some themes will display this post just fine, but others (the default wordpres twenty fifteen theme) are displaying this post and then every other post in the database after it. Im not sure where or why its pulling all of these posts, but if i can get it to stop it looks like this will be a valid solution.

Community
  • 1
  • 1
Dan Hastings
  • 3,241
  • 7
  • 34
  • 71
  • Not sure to understand. U want to add a template page from your plugin ? – Nozifel Jan 22 '16 at 08:55
  • not quite, but that is the end result i want. I want to be able to for example. generate the content of the page from the php script in my plugin and then use the current themes page.php to render my content. This way it will be in line with the themes css – Dan Hastings Jan 22 '16 at 08:56

1 Answers1

0

Then u could try to load a specific template page in specific case.

function load_my_template( $template )
{
    if( is_page() )
        $template = plugin_dir_path(__FILE__) . "dir_to/my_template.php";

    return $template;
}

Or change the content that is use on loading page

function load_my_content( $content )
{
    global $post;

    $id = $post->ID;

    if( is_page() )
    {
        ob_start();

        include plugin_dir_path(__FILE__) . "dir_to/my_template.php";

        $content = ob_get_clean();
    }

    return $content;
}

In your __construct()

add_filter('template_include', array($this,'load_my_template') );
add_filter("the_content", array($this,"load_my_content" ) );
add_filter("get_the_content", array($this,"load_my_content" ) );

Hope that help u. Tell me if it's not corresponding with your question.

Nozifel
  • 535
  • 2
  • 9
  • i need to have a dynamic system. i dont know the theme path or the template names. So i will just use the default single.php page. When i call require "...../single.php" it just loads the home page. not sure how to override the $post object to do what i want it to do. – Dan Hastings Jan 22 '16 at 09:23
  • the 'my_template.php' is a template page in your plugin that override the default template if is_page(). if you want to have a dynmic system, then remove if( is_page() ) and apply in all cases. – Nozifel Jan 22 '16 at 09:29
  • ive added an edit to my original comment to better explain where i am at right now. Im not sure how to clear the default wordpress variables before setting my own. when i call the theme page.php it has a bunch of stuff already in place – Dan Hastings Jan 22 '16 at 09:34