How to delete page after plugin deactivation - wordpress

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');

Related

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

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();
?>

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.

Set custom post default language

Set custom post default language.
I created a custom post with pods and I have WPML plugin installed.
I want to show this custom post only in a language which is not the default one of the website so that the urls will be http://example.com/en/postname instead of http://example.com/postname.
For this I would like when i create a new post of these custom posts that their default language should be english and not the default language of the website.
Otherwise I have every time to change the default language of the post for each post.
How can i set another default language for the custom post than the one of the website?
add_action('save_post', 'my_english_halacha');
function my_english_halacha($post_id) {
$post_type = get_post_type($post_id);
switch( $post_type )
{
case 'english_halacha':
$set_language_args = array(
'element_id' => $post_id,
'element_type' => 'post_english_halacha',
'language_code' => 'en',
'rewrite' => array('slug' => ( (ICL_LANGUAGE_CODE=='en') ) ),
'source_language_code' => 'he',
);
global $sitepress;
$sitepress->switch_lang('en');
do_action( 'wpml_set_element_language_details', $set_language_args );
break;
case 'spanish_halacha':
$set_language_args = array(
'element_id' => $post_id,
'element_type' => 'post_spanish_halacha',
'language_code' => 'es',
'rewrite' => array('slug' => ( (ICL_LANGUAGE_CODE=='es') ) ),
'source_language_code' => 'he',
);
global $sitepress;
$sitepress->switch_lang('es');
do_action( 'wpml_set_element_language_details', $set_language_args );
break;
}
}
Add the following action to functions.php, and I hope Problem will be Solved:
function update_post_language( $post_id ) {
$post_type = get_post_type($post_id);
if ($post_type == 'dwqa-question' || $post_type == 'dwqa-answer') {
$term = term_exists('ar', 'language');
if ($term !== 0 && $term !== null`enter code here`) {
wp_set_post_terms( $post_id, array ('ar'), 'language', true );
}
}
}
add_action( 'save_post', 'update_post_language' );

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 );
}

Wordpress: get_delete_post_link not working for custom role

I have a custom role in functions.php:
add_role('test_pilot', 'Test Pilot', array(
'read' => true,
'edit_posts' => true,
'delete_posts' => true,
));
// Give the custom role a new level
$test_pilot = get_role('test_pilot');
$test_pilot->add_cap('level_3');
...and on the front-end I'm trying to echo the delete post link:
<?php echo get_delete_post_link( get_the_ID() ); ?>
The problem is the link isn't actually being displayed when logged in as a user with the test pilot role.
If I am logged in as an administrator the link does display.
What am I doing wrong?
Try to replacing with below code:
function init_roles() {
global $wp_roles;
if (class_exists('WP_Roles'))
if ( ! isset( $wp_roles ) )
$wp_roles = new WP_Roles();
if (is_object($wp_roles)) :
$wp_roles->add_cap( 'editor');
endif;
$wp_roles->add_role( 'test_pilot', 'Test Pilot', array(
'read' => true,
'edit_posts' => true,
'delete_posts' => true
));
}
add_action('init', 'init_roles');

Resources