• Resolved webfantastic

    (@webfantastic)


    Please tell me how to add a value to the default subtitle field so that it is not empty if the manager forgot to write it there. For example, I need the title to be substituted there by default.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author HelgaTheViking

    (@helgatheviking)

    You could use conditional logic…

    if ( function_exists('get_the_subtitle') && get_the_subtitle() ) {
       the_subtitle();
    } else {
       the_title();
    }
    
    Thread Starter webfantastic

    (@webfantastic)

    Does not work. I tried to broadcast the subtitle to another plugin, but if the field is not filled in, it doesn’t show the title instead. How can I put the default value from title in the subtitle field itself when creating a product or article?

    Plugin Author HelgaTheViking

    (@helgatheviking)

    I don’t know exactly what you mean by broadcasting the subtitle to another plugin, but if you can share the subtitle you should be able to wrap it in conditional logic to check if it exists first.

    You might also be able to run something on save_post to save a value if none exists. I don’t really like this as it saves a bunch of default texts to the database, but it might do the trick, and is the best I can do.

    
    /**
     * Save a default Subtitle if none exists
     * 
     * @param  int $post_id the ID of the post we're saving
     * @return int
     */
    function kia_save_default_subtitle( $post_id ) {
        $subtitle = get_post_meta( $post_id, 'kia_subtitle', true );
        if ( $ subtitle ) {
            $default = esc_html__( 'A default subtitle', 'your-textdomain' );
            update_post_meta( $post_id, 'kia_subtitle', $default );
        }
    }
    add_action( 'save_post', 'kia_save_default_subtitle', 20 );

    `

    Thread Starter webfantastic

    (@webfantastic)

    Thank you, I only need this for groceries. I’ll try this option.

    Thread Starter webfantastic

    (@webfantastic)

    Can you tell me how to do this only for products? Is there a filter for this, or do I need to disable all the other subtitle in the plugin settings?

    Plugin Author HelgaTheViking

    (@helgatheviking)

    In theory, I think you could save on save_post_product… see: https://developer.www.ads-software.com/reference/hooks/save_post_post-post_type/

    
    /**
     * Save a default Subtitle if none exists
     * 
     * @param  int $post_id the ID of the post we're saving
     * @return int
     */
    function kia_save_default_subtitle( $post_id ) {
        $subtitle = get_post_meta( $post_id, 'kia_subtitle', true );
        if ( $ subtitle ) {
            $default = esc_html__( 'A default subtitle', 'your-textdomain' );
            update_post_meta( $post_id, 'kia_subtitle', $default );
        }
    }
    add_action( 'save_post_product', 'kia_save_default_subtitle', 20 );

    `

    Thread Starter webfantastic

    (@webfantastic)

    I tried, but it doesn’t work

    Plugin Author HelgaTheViking

    (@helgatheviking)

    Sorry, that’s the best I can offer. Though I did just notice that

    
    if ( $ subtitle ) {
    

    should be

    
    if ( $subtitle ) {
    

    Don’t know if that resolves your issue though. Alternatively, you could write additional conditional logic. With the post ID you can get_post_type( $post_id ) and maybe use something like that.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Default subtitle value’ is closed to new replies.