Adding a PHP page to WordPress via Plugin

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.

Read More

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.

Related posts

1 comment

  1. 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.

Comments are closed.