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',
) );
@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.
@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',
) );
@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.
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');