Created
April 12, 2023 09:00
-
-
Save apsolut/e8f78ed44dcc9e293c5eade86fae9e46 to your computer and use it in GitHub Desktop.
WordPress Auto Cleaner - Example to Delete Comments, Pages and create new Page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Delete pages | |
* @link https://developer.wordpress.org/reference/functions/wp_delete_post/ | |
*/ | |
function apsolut_delete_multiple_pages_from_plugin() { | |
// Set an array of page IDs to delete | |
$page_ids = array(1, 2, 3); // Hello World, Homepage, Privacy Policy | |
// Loop through the page IDs and delete each page | |
foreach ($page_ids as $page_id) { | |
// Check if the page exists | |
if (get_post_type($page_id) === 'page') { | |
// Delete the page from WordPress | |
wp_delete_post($page_id, true); | |
} | |
} | |
// Return success message | |
return 'Pages deleted successfully.'; | |
} | |
// Hook the function to a WordPress action | |
add_action('init', 'apsolut_delete_multiple_pages_from_plugin'); | |
/** | |
* Add page | |
* @link https://developer.wordpress.org/reference/functions/wp_insert_post/ | |
*/ | |
function apsolut_create_page_from_plugin() { | |
// Set the page title and content | |
$page_title = 'Your Page Title'; | |
$page_content = 'Your Page Content'; | |
// Create an array with the page details | |
$page_args = array( | |
'post_title' => $page_title, | |
'post_content' => $page_content, | |
'post_status' => 'publish', | |
'post_type' => 'page' | |
); | |
// Insert the page into WordPress | |
$page_id = wp_insert_post($page_args); | |
// Return the page ID | |
return $page_id; | |
} | |
// Hook the function to a WordPress action | |
add_action('init', 'apsolut_create_page_from_plugin'); | |
/** | |
* Remove all comments | |
* @link https://developer.wordpress.org/reference/functions/wp_delete_comment/ | |
*/ | |
function apsolut_remove_all_comments_from_plugin() { | |
// Get all comments | |
$comments = get_comments(); | |
// Loop through the comments and delete each comment | |
foreach ($comments as $comment) { | |
// Delete the comment from WordPress | |
wp_delete_comment($comment->comment_ID, true); | |
} | |
// Return success message | |
return 'All comments removed successfully.'; | |
} | |
// Hook the function to a WordPress action | |
add_action('init', 'apsolut_remove_all_comments_from_plugin'); | |
/** | |
* Remove emojis | |
*/ | |
function apsolut_remove_emojis() { | |
remove_action( 'wp_head', 'print_emoji_detection_script', 7 ); | |
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' ); | |
remove_action( 'wp_print_styles', 'print_emoji_styles' ); | |
remove_action( 'admin_print_styles', 'print_emoji_styles' ); | |
remove_filter( 'the_content_feed', 'wp_staticize_emoji' ); | |
remove_filter( 'comment_text_rss', 'wp_staticize_emoji' ); | |
remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' ); | |
add_filter( 'tiny_mce_plugins', 'disable_emojis_tinymce' ); | |
add_filter( 'wp_resource_hints', 'disable_emojis_remove_dns_prefetch', 10, 2 ); | |
} | |
add_action( 'init', 'apsolut_remove_emojis' ); | |
/** | |
* Others | |
*/ | |
remove_action('wp_head', 'rsd_link'); // Display the link to the Really Simple Discovery service endpoint, EditURI link | |
remove_action('wp_head', 'wlwmanifest_link'); // Display the link to the Windows Live Writer manifest file. | |
/** | |
* If dont need gutenberg | |
* not bad to be part of theme | |
*/ | |
add_action('wp_enqueue_scripts', function() { | |
wp_dequeue_style( 'wp-block-library' ); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment