WordPress.org

Ready to get started?Download WordPress

Forums

creating a custom code page for plugin (3 posts)

  1. imstevil
    Member
    Posted 1 year ago #

    I'm sorry for asking such a n00b question, but I'm having a lot of trouble finding this while searching (I'm also not 100% sure what I should be searching for).

    What I'm looking to do, is have a plugin where I can have a page setup where the content is generated purely by a php file. So still works with the template as normal, but all the output from that file is put into the content area.

    So if for example I had a php file which was just

    <?php
    print "this would be in the content area";
    ?>

    Then you would get a normal page with "this would be in the content area".

    I'm aware something like this could be done using wordpress files rather than creating my own custom files, but I'm interested in if it can be done in general, and if so how it's done incase I want to do more complex things which might be easier to manage doing it this way.

    I'm thinking this is probably something easy, but unfortunately I'm very new to word press plugins and have a client I do work for normally (not normally in wordpress) who is starting to ask for a few different things and I'm not sure where to start looking. As always with clients, they always let you know about a job when the deadline is right around the corner!

    If anyone can point me in the right direction, it would be greatly appreciated!

    Thank you in advance

  2. stvwlf
    Member
    Posted 1 year ago #

    You can use this filter
    http://codex.wordpress.org/Plugin_API/Filter_Reference/the_content
    to change the content of any WordPress post or page before it is displayed. You could for example create dummy blank pages and filter their content in PHP, replacing the existing content that's in the page with any output from any PHP function. Thus you could use a PHP "include" to include the output of an external PHP or HTML file, by assigning it to a variable in the filter function.

    add_filter( 'the_content', 'my_content_filter' );
    function my_content_filter( $content )
       if ( is_page( 'some-page-slug' ) {
          $content = 'this would be in the content area';
       } elseif ( is_page( 'another-page-slug' ) {
          $content = 'something else';
       }
       return $content;
    }

    This leaves the content of all posts and pages except for the two that are being reassigned as the post/page's default content.

    The function above doesn't have to go in a plugin (however, it can). You can put it in the theme's functions.php file

    See this for WordPress conditionals:
    http://codex.wordpress.org/Conditional_Tags

  3. imstevil
    Member
    Posted 1 year ago #

    Thanks stvwlf! I think that should do what I'm looking for, now I know what I'm searching for was able to do a bit more research on it and found this:-

    http://wordpress.org/support/topic/how-do-i-create-a-new-page-with-the-plugin-im-building

    Unfortunately I just woke up not long ago and running latel for work. When I get home I'll test it out and if I can manage it will post my finaly code for the problem... though I have a feeling it will be very similar to the code used here and in the other post.

Topic Closed

This topic has been closed to new replies.

About this Topic