• I’m working on my own theme which displays excerpts on the home page, and I’ve created an option that allows users to select their own “Read More” text at the end of excerpts.

    This is in my functions.php file:

    // Change "read more" link on excerpts
    // codex.wordpress.org/Function_Reference/the_excerpt
    function greybox_excerpt_more( $more ) {
    	$text = get_theme_mod( 'gb_readmore', '…read more…' );
    	return ' <a class="excerpt-link" href="' . get_permalink( get_the_ID() ) . '">' . $text . '</a>';
    }
    add_filter( 'excerpt_more', 'greybox_excerpt_more' );

    and this is what I added to my customizer.php file:

    $wp_customize->add_setting( 'gb_readmore', array(
    		'type'               => 'theme_mod',
    		'transport'          => 'postMessage',
    		'default'            => __( '&hellip;read more&hellip', 'greybox' ),
    		'sanitize_callback'  => 'greybox_sanitize_text',
    	) );
    
    	$wp_customize->add_control( 'gb_readmore', array(
    		'section'     => 'greybox_post_format_excerpt_options',
    		'type'        => 'text',
    		'label'       => __( 'Text for &lsquo;read more&rsquo; link', 'greybox'),
    	) );

    Everything works well. If you don’t use that option in the customizer, the excerpt is linked to with “…read more…” which is the default. If you add text to that option in the customizer, that text shows up in the excerpts.

    The problem is if you delete your custom text in the customizer, no link is shown at the end of the excerpts, even though I specify a default in both the functions and customizer file.

    I have, in fact, tried adding a default to just the customizer file, and also adding a default to just the functions file, but the result was the same.

    The code from greybox_excerpt_more() does show up in the text of the post, there’s just nothing specified as an anchor text:

    <a class="excerpt-link" href="http://127.0.0.1/wp-dev/2015/07/24/this-is-a-sticky-post/"></a>

    It is not just this particular setting, either. I have another setting that allows users to add custom copyright text to the footer, which defaults to a CC4 license if they leave it blank. It works, but if you delete your custom copyright information, it just leaves a blank space and doesn’t revert to the default.

    I have googled this quite a bit and not found a resolution. The customizer options do work, so I’m doing something right. I just can’t figure out this last bit.

Viewing 1 replies (of 1 total)
  • Thread Starter kjodle

    (@kjodle)

    I think this resolves it:

    Instead of using my own sanitization function, I used a built-in WordPress one, instead. So this line:

    'sanitize_callback' => 'greybox_sanitize_text',

    became this line:

    'santize' => 'html',

Viewing 1 replies (of 1 total)
  • The topic ‘Theme customizer options don't reset to default’ is closed to new replies.