Is there any way to use default_content
only for a certain post type, specifically product
(WooCommerce)?
My code:
add_filter( 'default_content', 'set_default_content', 10, 2 );
function set_default_content( $content, $product ) {
$content ='content to add to a post']';
return $content;
}
I've tried with if ( 'product' == get_post_type() )
or even if ( 'page' == get_post_type() )
to test it but it's not working.
Is there any way to use default_content
only for a certain post type, specifically product
(WooCommerce)?
My code:
add_filter( 'default_content', 'set_default_content', 10, 2 );
function set_default_content( $content, $product ) {
$content ='content to add to a post']';
return $content;
}
I've tried with if ( 'product' == get_post_type() )
or even if ( 'page' == get_post_type() )
to test it but it's not working.
default_content
is a filter used in the backend. You don't necessarily have anything in the loop so standard functions will probably fail. However, you're given a second argument of type WP_Post
. You can check its post_type
easily and work from there.
add_filter('default_content', 'WPSE_product_default_content', 10, 2);
function WPSE_product_default_content($post_content, $post) {
if ($post->post_type !== 'product')
return $post_content;
$content ='content to add to a post'
return $content;
}