woocommerce offtopic - Filter default_content only for products

admin2025-06-06  2

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.

Share Improve this question edited Nov 27, 2018 at 18:15 kero 6,3501 gold badge25 silver badges34 bronze badges asked Nov 27, 2018 at 18:02 Sergiu ElmiSergiu Elmi 1033 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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;
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749147652a316772.html

最新回复(0)