logo

$25
replace a text by another in all custom field

with this code, "<div>this text</div>" is replaced by "<span>another text</span>" in all post_content


add_filter('the_content', 'replace_this_text');
add_filter('the_excerpt', 'replace_this_text');

function replace_this_text(){
$content = get_the_content($post->ID);
$content = str_replace('<div>this text</div>','<span>another text</span>',$content);
return $content;
}


i want to do the same thing in all custom_fied


Sébastien | French WordpressDesigner | 05/30/11 at 10:44am | Edit


(8) Possible Answers Submitted...

  • avatar
    Last edited:
    05/30/11
    10:58am
    Tamilmozhi Gunasekar says:

    Hi..
    You can use


    add_filter('get_post_metadata', 'replace_this_text');


    Also I guess the code

    function replace_this_text(){
    $content = get_the_content($post->ID);
    $content = str_replace('<div>this text</div>','<span>another text</span>',$content);
    return $content;
    }


    Should be

    function replace_this_text($content){
    $content = get_the_content($post->ID);
    $content = str_replace('<div>this text</div>','<span>another text</span>',$content);
    return $content;
    }

    Previous versions of this answer: 05/30/11 at 10:56am | 05/30/11 at 10:58am

    • 05/30/11 11:00am

      Sébastien | French WordpressDesigner says:

      what the difference between the first code and the second ?

    • 05/30/11 11:02am

      Tamilmozhi Gunasekar says:

      The first line


      function replace_this_text(){

      function replace_this_text($content){

    • 05/30/11 11:12am

      Sébastien | French WordpressDesigner says:

      but get_the_content is ok to display the content, not to display the custom field, isn't it ?

    • 05/30/11 11:16am

      Tamilmozhi Gunasekar says:

      Yes, You can just use get_the_content without using the $content attribute in the function or just use $content attribute and not use get_the_content line.


      function replace_this_text(){
      $content = get_the_content($post->ID);
      $content = str_replace('<div>this text</div>','<span>another text</span>',$content);
      return $content;
      }


      or


      function replace_this_text($content){
      $content = str_replace('<div>this text</div>','<span>another text</span>',$content);
      return $content;
      }

    • 05/30/11 11:25am

      Sébastien | French WordpressDesigner says:

      that don't work :-/

  • avatar
    Last edited:
    05/30/11
    11:04am
    Erez S says:

    In the custom fields filter you don't get the content as a parameter,so you have to get it manually using this line:
    $content = get_the_content($post->ID);

    BTW, I think you should use this code:


    add_filter('get_post_metadata', 'replace_this_text_custom_fields');
    function replace_this_text_custom_fields(){
    global $post;
    $content = get_the_content($post->ID);
    $content = str_replace('<div>this text</div>','<span>another text</span>',$content);
    return $content;
    }

    Previous versions of this answer: 05/30/11 at 11:04am

    • 05/30/11 11:06am

      Erez S says:

      Anyway, you can use this plugin:
      http://wordpress.org/extend/plugins/search-and-replace/
      And to replace it inside all the posts, and then you won't have to add this filter

    • 05/30/11 11:16am

      Sébastien | French WordpressDesigner says:

      but get_the_content is ok to display the content, not to display the custom field, isn't it ?

    • 05/30/11 11:30am

      Sébastien | French WordpressDesigner says:

      that don't work :-/ another idea ?

    • 05/30/11 11:39am

      Erez S says:


      add_filter('get_post_metadata', 'replace_text_custom_meta', 10, 4);

      function replace_text_custom_meta($content, $postid, $metakey,$single) {
      if($single)
      return str_replace('<div>this text</div>','<span>another text</span>',get_post_meta($postid, $metakey, true));
      return get_post_meta($postid, $metakey, false);
      }


      Try this

    • 05/30/11 11:39am

      Erez S says:


      add_filter('get_post_metadata', 'replace_text_custom_meta', 10, 4);

      function replace_text_custom_meta($content, $postid, $metakey,$single) {
      if($single)
      return str_replace('<div>this text</div>','<span>another text</span>',get_post_meta($postid, $metakey, true));
      return get_post_meta($postid, $metakey, false);
      }


      Try this

    • 05/30/11 11:41am

      Sébastien | French WordpressDesigner says:

      it seems that don't work

    • 05/30/11 12:07pm

      Erez S says:


      add_action('wp_head', 'replace_text_custom_meta_head');

      function replace_text_custom_meta_head() {
      global $post;
      $customs = all_my_customs($post->ID);
      print_r($customs);
      foreach($customs as $k=>$v){
      update_post_meta($post->ID, $k, replace_this_text($v));
      }
      update_option('meta_cache',serialize($customs));
      }

      add_action('wp_footer', 'replace_text_custom_meta_footer');

      function replace_text_custom_meta_footer($content, $postid, $metakey,$single) {
      global $post;
      $customs = unserialize(get_option('meta_cache'));
      foreach($customs as $k=>$v){
      update_post_meta($post->ID,$k, replace_this_text($v));
      }
      }

      function all_my_customs($id = 0){
      //if we want to run this function on a page of our choosing them the next section is skipped.
      //if not it grabs the ID of the current page and uses it from now on.
      if ($id == 0) :
      global $wp_query;
      $content_array = $wp_query->get_queried_object();
      $id = $content_array->ID;
      endif;

      //knocks the first 3 elements off the array as they are WP entries and i dont want them.
      $first_array = @array_slice(get_post_custom_keys($id), 3);

      //first loop puts everything into an array, but its badly composed
      foreach ($first_array as $key => $value) :
      $second_array[$value] = get_post_meta($id, $value, FALSE);

      //so the second loop puts the data into a associative array
      foreach($second_array as $second_key => $second_value) :
      $result[$second_key] = $second_value[0];
      endforeach;
      endforeach;

      //and returns the array.
      return $result;
      }

      add_filter('the_content', 'replace_this_text');
      add_filter('the_excerpt', 'replace_this_text');
      function replace_this_text($content)
      {
      return str_replace('screenshot', '<span>another text</span>', $content);
      }

      This works 100%

    • 05/30/11 12:20pm

      Sébastien | French WordpressDesigner says:

      not in my site (wp3.0.1)
      because i use some plugins ?

    • 05/30/11 12:45pm

      Erez S says:

      Make sure you have
      <?php wp_head(); ?>
      inside the <head> tag, and
      <?php wp_footer(); ?>
      Before the closing of the <body> tag. Otherwise it won't work

    • 05/30/11 12:48pm

      Erez S says:

      BTW, where do you put the code? You have to put it inside the functions.php code, otherwise it won't work

    • 05/30/11 12:50pm

      Sébastien | French WordpressDesigner says:

      al is weel but that don't work.
      have you test in wp3.0.1 ?

    • 05/30/11 12:51pm

      Erez S says:

      Yes...
      Can you send me the whole theme to my email erezaton213@gmail.com ?

  • avatar
    Last edited:
    05/30/11
    11:13am
    AdamGold says:

    function replace_me($content) {
    $content = str_replace('<div>this text</div>','<span>another text</span>',$content);
    return $content;
    }

    add_filter('get_post_metadata', 'replace_me');

    • 05/30/11 11:16am

      Sébastien | French WordpressDesigner says:

      don't work

    • 05/30/11 11:31am

      AdamGold says:

      Only way I can think of is to make another function instead of get_post_meta

      function gold_get_post_meta($name, $post_id) {
      global $post;
      $custom_field = get_post_meta($name, $post_id, true);
      $custom_field = str_replace('<div>this text</div>','<span>another text</span>',$value);
      return $custom_field;
      }


      Then call gold_get_post_meta

    • 05/30/11 11:33am

      Sébastien | French WordpressDesigner says:

      that's an idea... but that's not really what i need...

  • avatar
    Last edited:
    05/30/11
    11:41am
    Utkarsh Kukreti says:

    There's no filter on custom fields that returns the field value, so I couldn't think of any straightforward way.

    Try this out (Uses code from WP 3.1 source, should work in 3.1 or later).


    add_filter('get_post_metadata', 'filter_post_meta', 10, 4);


    function filter_post_meta($nothing, $object_id, $meta_key, $single)
    {

    $meta_cache = wp_cache_get($object_id, 'post' . '_meta');

    if (!$meta_cache) {
    $meta_cache = update_meta_cache('post', array($object_id));
    $meta_cache = $meta_cache[$object_id];
    }

    if (!$meta_key)
    return $meta_cache;

    if (isset($meta_cache[$meta_key])) {
    if ($single)
    return replace_this_text(maybe_unserialize($meta_cache[$meta_key][0]));
    else
    return array_map('maybe_unserialize', $meta_cache[$meta_key]);
    }

    if ($single)
    return '';
    else
    return array();
    }

    function replace_this_text($content)
    {
    return str_replace('<div>this text</div>', '<span>another text</span>', $content);
    }

    Previous versions of this answer: 05/30/11 at 11:41am

  • avatar
    Last edited:
    05/30/11
    12:02pm
    Spencer says:

    As Utkarsh Kukreti said, there are no direct filters when outputting custom post meta. So, the easiest way may be to just replace your get_post_meta() call in the template file, with a custom function. Try putting this in functions.php:

    function sebastien_fix_meta() {
    global $post;

    $content = get_post_meta( $post->ID, 'adresse1', true );
    $content = str_replace( '<div>this text</div>', '<span>another text</span>', $content );

    return $content;
    }


    And then instead of calling:

    <?php echo "test : " .  get_post_meta($post->ID, 'adresse1', true); ?>


    Do:

    Test: <?php echo sebastien_fix_meta(); ?>

    Previous versions of this answer: 05/30/11 at 12:02pm

  • avatar
    Last edited:
    05/30/11
    12:11pm
    Just Me says:

    Not sure why you want that text changed but I guess it is too much trouble to change it manually in every post.

    Isn't it more logical to change it in the database so you don't have to execute a function every time a page loads?

  • avatar
    Last edited:
    05/30/11
    12:50pm
    Tom Ransom says:

    If this is a permanent change, then as Just Me indicated - change the database:

    1) Backup your database (at least `postmeta`)
    2) Backup your database - just in case you skipped step 1
    3) No really, back it up

    Use phpMyAdmin to access your specific database and go to the sql tab and use:

    UPDATE `wp_postmeta` SET `meta_value` = REPLACE(`meta_value`, '<div>this text</div>', '' ) WHERE `meta_key` = 'YOUR CUSTOM META FIELD NAME';

    IMPORTANT NOTES:
    See steps 1-3 above
    Your prefix might not be wp_
    There are two kinds of quote marks above: back quotes surround the field names and vertical single quotes surround the data.

    And did I mention, make a backup.

    - Tom

  • avatar
    Last edited:
    06/01/11
    3:35am

    This answer was downvoted by the top experts.

This question has expired.





Current status of this question: Community pot