• Resolved jimmyjumpdrive

    (@jimmyjumpdrive)


    So, I’ve been searching around and can’t seem to find an answer, though I’ve seen this done before. I’m developing a plugin and am trying to figure out how to auto-generate a page upon plugin activation, and deactivate that page during deactivation. I already have the hooks setup, it’s simply creating and deactivating the page I’m having trouble with.

    I’ve searched around and can’t seem to find any help in the documentation, simply linking me to examples, resource, or documentation should suffice,

    Thanks!

    ===== Edit ======
    I forgot to mention, this page must have a custom template, it’s going to be an empty page with a single large custom widget object.

Viewing 7 replies - 1 through 7 (of 7 total)
  • You can use wp_insert_post(). You just need to specify post_type in the arguments as page:

    wp_insert_post( array(
      'post_title'    => 'My new page',
      'post_type'     => 'page',
      'post_status'   => 'publish',
    ) );
    
    Thread Starter jimmyjumpdrive

    (@jimmyjumpdrive)

    @jakept
    Thank you, I actually just found this and edited my post to elaborate more on the problem. Is there any way i can use this with a custom template?

    Well the trouble is you mentioned that this is a plugin, which means you won’t know what templates the theme will have available.

    You can pass the template filename to page_template though:

    wp_insert_post( array(
      'post_title'    => 'My new page',
      'post_type'     => 'page',
      'post_status'   => 'publish',
      'page_template' => 'custom-template.php',
    ) );
    

    But it will throw an error if the template doesn’t exist. You could handle the template setting separately, by checking for it and setting it after the post has been inserted:

    $post_id = wp_insert_post( array(
      'post_title'    => 'My new page',
      'post_type'     => 'page',
      'post_status'   => 'publish',
    ) );
    
    if ( $post_id && ! is_wp_error( $post_id ) ) {
      $page_templates = get_page_templates();
    
      if ( isset( $page_templates['custom-template.php'] ) ) {
      update_post_meta( $post_id, '_wp_page_template', 'custom-template.php' );
      } 
    }
    

    A custom template probably isn’t the way to go though if you want to remain theme agnostic. Another option could be that you create a shortcode and make that the post content of your new page.

    • This reply was modified 8 years, 10 months ago by Jacob Peattie.
    • This reply was modified 8 years, 10 months ago by Jacob Peattie.
    • This reply was modified 8 years, 10 months ago by Jacob Peattie.
    Thread Starter jimmyjumpdrive

    (@jimmyjumpdrive)

    @jakept Thanks, I looked into the shortcode thing, but since there are no options for my widget, I actually think the best route was to generate an instance of my widget using the_widget() method, since the only content on the page is the widget, i tried to just set the content field of my post to the_widget(‘My_Wp_Widget’);

    but, I don’t think it works, I’m gonna toy with it a bit, if it doesn’t work out, i’ll try using the shortcodes.

    
    $callback_page = array(
        'post_title'	=> wp_strip_all_tags('MyPlugin | Callback'),
        'post_content'	=> the_widget( 'My_Widget_Class'),
        'post_type'		=> 'page',
        'post_status'	=> 'public',
        'post_author'	=> 1
    );
    
    wp_insert_post($callback_page);
    

    This is the hook I’m using:

    
    register_activation_hook( __FILE__, array( 'MyPlugin', 'import_custom_pages' ) );
    

    The problem with that is that the_widget() echos the output, so it’ll just print the widget to screen on plugin activation. You could capture the output and insert that as content, but that would just be hardcoding the widget output at the instant the plugin was activated, rather than properly rendering the widget.

    You can create a simple shortcode that outputs an instance of your widget, and make that the content though:

    function my_widget_shortcode() {
        ob_start();
        the_widget( 'My_Widget_Class' );
        return ob_get_clean();
    }
    add_shortcode( 'my_widget', 'my_widget_shortcode' );
    
    $post_id = wp_insert_post( array(
      'post_title'    => 'My new page',
      'post_content'  => '[my_widget]',
      'post_type'     => 'page',
      'post_status'   => 'publish',
    ) );
    
    • This reply was modified 8 years, 10 months ago by Jacob Peattie.
    Thread Starter jimmyjumpdrive

    (@jimmyjumpdrive)

    @jakept, I’ve been toying with the example you gave, the page is showing up but the contents of the page simply shows the shortcode [tds_callback] (being the shortcode i registered).

    
    // [File: class.tds-tickets.php]
    
    // called by activation hook, registered in main
    public static function plugin_activation()
    {
        // Tds_Tickets being the name of the class of the current file
        Tds_Tickets::import_widget_shortcodes();
        Tds_Tickets::import_custom_pages();
    }
    
    private static function import_custom_pages()
    {
        $callback_page = array(
    	'post_title'	=> wp_strip_all_tags('Tds Tickets | Callback'),
    	'post_content'	=> '[tds_callback]',
    	'post_type'		=> 'page',
            'post_status'	=> 'publish',
            'post_author'	=> 1
        );
        $post_id = wp_insert_post($callback_page);
    }
    
    private static function import_widget_shortcodes()
    {
        add_shortcode( 'tds_callback', 'instantiate_callback_widget' );
    }
    
    private static function instantiate_callback_widget()
    {
        ob_start();
        the_widget('Tds_Search_Widget');
        return ob_get_clean();
    }
    

    ======= EDIT =======
    Update on the matter, it seems that it will post the shortcode’s text instead of the function’s return regardless if i’m grabbing the_Widget or just pringing html.

    Something I test that failed:

    
    private static function instantiate_callback_widget()
    {
        ob_start();
        ?>
            <p>This is the callback Widget Placeholder</p>
        <?php
        return ob_get_clean();
    }
    

    ===== EDIT ======
    Defining my shortcode in my “main” php file works, but not from my hooks.

    Thread Starter jimmyjumpdrive

    (@jimmyjumpdrive)

    I have resolved the issue, the add_shortcode method would not work in my plugin activation hook, i had to move it over to an active php, such as my main. The functions for rendering these objects remained in the plugin’s class method.

    
    add_shortcode( 'MyWidget', array('My_Plugin_Class_Name', 'ShortCodeMethod');
    
Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Custom Plugin, Auto Generating Pages’ is closed to new replies.