0

I want to repeat my title/keyword in different places in my content.

for example, my title is "Dog"

I want to replace "Dog" everywhere I have inserted *** or any hint.

how this is possible easily?

1 Answer 1

1

You can search-replace those placeholders with the_content filter.

function custom_the_content( $content ) {
    return str_replace('{placeholder}', get_the_title(), $content);
}
add_filter('the_content', 'custom_the_content');

Note: I don't recommend using *** or similar as a placeholder, as it can be commonly used. That is why I've used {placeholder}.

UPDATE: Based on the comments below, if you need to search-replace multiple values within the_content filter, use arrays as str_replace arguments like below:

function custom_the_content( $content ) {

    // Search for these elements
    $search = array(
        '{apktitle}',
        '{apkversion}',
    );

    // And replace them with these (the order of array items is preserved)
    $replace = array(
        get_the_title(),
        get_datos_info('version'),
    );

    return str_replace( $search, $replace, $content );
}
add_filter('the_content', 'custom_the_content');
8
  • Do i need to put this code in Functions.php and (placeholder) should be text which i want to replace with title? Thanks bro, please confirm how to use this code. Just adding in functions.php will work? Commented Jul 2, 2022 at 11:07
  • @MuhammadImran Yes, adding this code to functions.php will work. Yes, {placeholder} is the text that will be replaced with the post title. Commented Jul 6, 2022 at 5:49
  • @Filo, can you can please give any hint to create any field like Title. I want to replace multiple values with multiple placeholders. Commented Jul 11, 2022 at 14:25
  • i have created a custom field using plugin, field name is app_version, how can call it as we can call title ? Commented Jul 11, 2022 at 14:35
  • If the custom plugin you are referring to is Advanced Custom Fields, then you would call it like this: get_field('app_version'). However this is very well documented in official ACF documentation and it is out of scope of this stack, you should check it out. Commented Jul 12, 2022 at 5:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.