WordPress Development Asked by Michael Rogers on January 14, 2021
I’m hooking to sanitize_title.
How can i check if post being edited is page? I mean the type.
As @Jacob mentioned, is an generic utility function used in many contexts and therefore, you might want to either use other approach or hook it selectively through other hook where you would have $post
object (e.g. edit_post
or other way to define if the post edited is of page type.
Something along these lines:
<?php
// sanitizing function.
function wpse341230_remove_false_words($slug) {
if (!is_admin()) return $slug;
$keys_false = array('a','and','above','the','also');
$slug = explode ('-', $slug);
$slug = array_diff($slug,$keys_false);
return implode ('-', $slug);
}
// array of post types where sanitizing function should be hooked;
$types = array(
'page',
'product',
);
// hook the following function a hook with $post object. e.g. edit_post or save_post.
add_action('edit_post','wpse341230_hook_sanitize_title', 10, 2);
// this function checks if the post type is one we want to sanitize the title.
function wpse341230_hook_sanitize_title($post_ID, $post, $types){
// if the $post->post_type property is in the array of types for sanitizing, hook the sanitizing function
if( in_array($post->post_type, $types) ){
add_filter('sanitize_title','wpse341230_remove_false_words');
}
}
?>
I would advise to check the available hooks in the Actions Run During an Admin Page Request section of this documentation page to check the typically available hooks for you.
Answered by Celso Bessa on January 14, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP