How do I add a new page using code thru a Wordpress plugin? - wordpress

I followed the chosen answer here -> How to create new page in wordpress plugin?
and I added the following code in a new Wordpress plugin folder and file and then activated in the Wordpress admin menu. Yet I don't have a new page created when I go to the slug demosite.com/custom/
add_action( 'admin_menu', 'register_newpage' );
function register_newpage(){
add_menu_page('custom_page', 'custom', 'administrator','custom', 'custompage');
remove_menu_page('custom');
}
Do I have to do something special to make my Wordpress plugin code work? I really need to be able to add a new page using my plugin functionality.

For create fronted page when plugin activation used register_activation_hook() like below.
register_activation_hook() function registers a plugin function to be run when the plugin is activated.
The first thing we do on activation is check that the current user is allowed to activate plugins. We do this using the current_user_can function
Finally, we create our new page, after we check that a page with the same name does not exist
register_activation_hook( __FILE__, 'register_newpage_plugin_activation' );
function register_newpage_plugin_activation() {
if ( ! current_user_can( 'activate_plugins' ) ) return;
global $wpdb;
if ( null === $wpdb->get_row( "SELECT post_name FROM {$wpdb->prefix}posts WHERE post_name = 'new-page-slug'", 'ARRAY_A' ) ) {
$current_user = wp_get_current_user();
// create post object
$page = array(
'post_title' => __( 'New Page' ),
'post_status' => 'publish',
'post_author' => $current_user->ID,
'post_type' => 'page',
);
// insert the post into the database
wp_insert_post( $page );
}
}
Here is a full list of parameters accepted by the wp_insert_post function
After plugin active successfully you can access your page using demosite.com/new-page-slug/

I'm not sure if you intended for the page to be created only once if so you should do it during plugin activation.
You might want to consider the following pseudo-ish code:
register_activation_hook( __FILE__, 'moveFile' );
function moveFile(){
if( check if post exists ){
wp_insert_post() # obviously title is "whatever", following convention
#move the file to themes folder
$source = plugin_dir_path(__FILE__) . "page-whatever.php";
$destination = get_template_directory() . "/page-whatever.php";
$cmd = 'cp ' . $source . ' ' . $destination;
exec($cmd);
}
}
It's similar to the code answered by Ankur, but this sample let you have a custom page. Caveat, my method uses exec() command.
I hope this helps.

/* If you want create a page with syn page template on plugin activation so see below example */
register_activation_hook( __FILE__, 'activate' );
function activate() {
$the_slug = 'our-services';
$args = array(
'name' => $the_slug,
'post_type' => 'post',
'post_status' => 'publish',
'numberposts' => 1
);
$my_posts = get_posts($args);
if(empty($my_posts)){
$my_post = array(
'post_title' => 'Our Services',
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'page',
'post_name' => 'our-services'
);
// Insert the post into the database
$post_id=wp_insert_post( $my_post );
update_post_meta( $post_id, '_wp_page_template', 'page-templates/our-services.php' );
}
}
/* page-template - our-services.php* /
<?php
/*
* Template Name: our-services
*/
get_header();
get_footer();
?>

Related

WordPress XMLRPC - Search Post by Slug and Update

Is there anyway to search posts by slug through XMLRPC
https://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.getPosts
getPosts() doesn't seem to return using "name"..
$args = array(
'name' => 'my-slug',
'number' => 1
);
$post = $wpClient->getPosts( $args );
Please let me know if there is a workaround for this, I need to search by slug and then update those slugs remotely via XMLRPC. cheers
I ended up using Methods, this may help someone and save time.. paste the following code in functions.php of the domain you are fetching data from
add_filter('xmlrpc_methods', 'clx_xmlrpc_methods');
function clx_xmlrpc_methods($methods) {
$methods['getPostBySlug'] = 'clx_getpost';
return $methods;
}
function clx_getpost($args) {
global $wp_xmlrpc_server;
$slug = $args["slug"];
$pargs = array(
'name' => $slug,
'post_type' => 'post',
'numberposts' => 1
);
$my_posts = get_posts($pargs);
if( $my_posts ) :
return $my_posts; //echo $my_posts[0]->ID;
endif;
}
from your XMLRPC code use the following to get POST array from slug
$args = array(
'slug' => 'your-post-slug'
);
$postArray = $wpClient->callCustomMethod( 'getPostBySlug', $args );

Creating a page with wp_insert_post()

I have a form that collects data about a user's product and then creates a page in WordPress with
$my_post = array(
'post_content' => "My page content",
'post_title' => $product_title,
'post_name' => $product_title,
'post_type' => 'page', // must be 'page' to accept the 'page_template' below
'page_template' => "listing.php",
'post_status' => "publish"
);
$ID = wp_insert_post( $my_post );
$permalink = get_permalink($ID);
echo "<br />ID for new page is $ID, Permalink for new page is $permalink";
The form data is put into meta variables for the page ID and the listing.php template file pulls it out of there and builds the HTML to display the product page. This all works fine and I can see that the page meta variable, _wp_page_template, gets set correctly to the template file I specified, listing.php:
Now I want to create a second page from the same form data, this one displaying different parts of the data in a different way. So I've added a second block of code, starting at $my_cert below, that creates this second page and specifies a different template, certificate.php, that knows how to build the second version of the data.
$my_post = array(
'post_content' => "My page content",
'post_title' => $product_title,
'post_name' => $product_title,
'post_type' => 'page', // must be 'page' to accept the 'page_template' below
'page_template' => "listing.php",
'post_status' => "publish"
);
$ID = wp_insert_post( $my_post );
$permalink = get_permalink($ID);
echo "<br />ID for new page is $ID, Permalink for new page is $permalink";
$my_cert = array(
'post_content' => "My certificate", // post_content is required
'post_title' => "My certificate", // post_title is required
'post_name' => "My certificate",
'post_type' => 'page', // must be 'page' to accept the 'page_template' below
'page_template' => "certificate.php",
'post_status' => "publish"
);
$CERT_ID = wp_insert_post( $my_cert );
$cert_permalink = get_permalink($CERT_ID);
echo "<br />ID for new certificate is $CERT_ID, Permalink for new certificate is $cert_permalink";
But when I look in the meta data for the second page created, the template is set to "default" instead of certificate.php:
I know I've set up certificate.php correctly as a template (set /* Template Name: certificate */ at the top) because the Page Edit Template dropdown includes certificate:
So does anyone see why I can't create this second page with the template set to certificate.php?
Thanks
Are you sure your page template src for: certificate.php is: certificate.php? And not: templates/certificate.php or something like that. Look in your theme folder and be 100% of the page template path. Check your spelling or for typos in the page template path or name. It must be an exact match.
If you still have problems I would look into and debug the source code of: wp_insert_post()
if ( ! empty( $postarr['page_template'] ) && 'page' == $data['post_type'] ) {
$post->page_template = $postarr['page_template'];
$page_templates = wp_get_theme()->get_page_templates( $post );
if ( 'default' != $postarr['page_template'] && ! isset( $page_templates[ $postarr['page_template'] ] ) ) {
if ( $wp_error ) {
return new WP_Error('invalid_page_template', __('The page template is invalid.'));
}
update_post_meta( $post_ID, '_wp_page_template', 'default' );
} else {
update_post_meta( $post_ID, '_wp_page_template', $postarr['page_template'] );
}
}
So its probably this part that fails:
if ( 'default' != $postarr['page_template'] && ! isset( $page_templates[ $postarr['page_template'] ] ) )
Try to modify: wp-includes/post.php and go to the definition of: function wp_insert_post() on row: 2872. And add a new row on row: 3312 for debugging purposes.
echo '<pre>';
print_r( $page_templates );
echo '</pre>';
die();
Make sure your certificate.php is among those in that array. Remember to delete the debug code before continuing. This should give you some answers.

How to delete page after plugin deactivation

I have created page when my plugin is activated. Its working fine. Now i want to delete the page when my plugin is deactivated.
My code is given below :
register_activation_hook( __FILE__, 'my_plugin_install_function');
function my_plugin_install_function() {
$post = array('page_template' => '', 'comment_status' => 'closed', 'ping_status' => 'closed' ,'post_author' => 1,'post_date' => date('Y-m-d H:i:s'),'post_name' => 'Checklists','post_status' => 'publish' ,
'post_title' => 'Checklists',
'post_type' => 'page',
);//insert page and save the id
$newvalue = wp_insert_post( $post, false );
//save the id in the database
update_option( 'hclpage', $newvalue ); }
register_deactivation_hook( __FILE__, 'my_plugin_remove' );
function my_plugin_remove() {// the id of our page...
$the_page_id = get_option( $newvalue );
if( $the_page_id ) {
wp_delete_post( $the_page_id ); // this will trash, not delete
}
How can I get the post id to delete the page?
wp_delete_post( $the_page_id, true );
The second parameter is to "force deletion", is boolean, and when set to true it deletes the post without trashing it.
You can read more in the docs
You can get the ID using the get_option function:
get_option('hclpage');

Wordpress: wp_insert_post() firing multiple times

I'm building a simple WP plugin that's supposed to create a new post and save some meta for the post. I created a function with the functionality and for the time being I hooked it to the 'init' event to check if it works.
add_action( 'init', 'my_func' );
function my_func() {
$my_post = array(
'post_title' => 'Some Post Title',
'post_name' => 'some-post-title',
'post_type' => 'custom-post-type',
'post_status' => 'publish'
);
$inserted_post_id = wp_insert_post($my_post);
if($inserted_post_id != 0) {
add_post_meta($inserted_post_id, 'some-key', 'some-value');
add_post_meta($inserted_post_id, 'some-other-key', 'some-other-value');
echo 'SUCCESS';
} else {
echo 'ERROR';
}
}
Now, whenever I reload the admin page, I get the 'SUCCESS' message echoed out, and I also get 4-6 new post called 'Some Post Title', and 4-6*2 new entries in the postmeta table. The 'SUCCESS' message echoes only once, meaning the function runs only once, still I get the data inserted to the database multiple times. What am I doing wrong?
Check before going to insert new post if already exists or not. So get_page_by_title('Some Post Title') == false then only insert new post.
add_action( 'init', 'my_func' );
function my_func() {
$my_post = '';
if( get_page_by_title('Some Post Title','OBJECT','custom-post-type') == NULL )
$my_post= array(
'post_title' => 'Some Post Title',
'post_name' => 'some-post-title',
'post_type' => 'custom-post-type',
'post_status' => 'publish'
);
wp_insert_post( $my_post );
}

Creating a post using PHP in one of the blogs in WP MU

I have a PHP program to create a site/blog in a networking enabled WordPress. After the site creation, I want to create a post using the same PHP program.
If I use wp_insert_post() function, it's creating the post in the main site/blog, not in the new site/blog I created. I also tried using switch_to_blog() before calling the wp_insert_post() but no luck.
I got the answer for this... The culprit is $blog_id, the moment I changed the variable name to $new_blog_id, it started working. Thanks
The following used as Must Use plugin does the job:
<?php
/* Plugin Name: New Post on Site Creation */
add_action( 'wpmu_new_blog', 'default_post_so_5334372', 10, 6 );
function default_post_so_5334372( $blog_id, $user_id, $domain, $path, $site_id, $meta )
{
switch_to_blog( $blog_id );
$my_post = array(
'post_title' => 'My post',
'post_content' => 'This is my post.',
'post_status' => 'publish',
'post_author' => 1
);
wp_insert_post( $my_post );
restore_current_blog();
}

Resources