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
Tamilmozhi Gunasekar answers:
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;
}
Sébastien | French WordpressDesigner comments:
what the difference between the first code and the second ?
Tamilmozhi Gunasekar comments:
The first line
function replace_this_text(){
function replace_this_text($content){
Sébastien | French WordpressDesigner comments:
but get_the_content is ok to display the content, not to display the custom field, isn't it ?
Tamilmozhi Gunasekar comments:
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;
}
Sébastien | French WordpressDesigner comments:
that don't work :-/
Erez S answers:
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;
}
Erez S comments:
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
Sébastien | French WordpressDesigner comments:
but get_the_content is ok to display the content, not to display the custom field, isn't it ?
Sébastien | French WordpressDesigner comments:
that don't work :-/ another idea ?
Erez S comments:
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
Erez S comments:
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
Sébastien | French WordpressDesigner comments:
it seems that don't work
Erez S comments:
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%
Sébastien | French WordpressDesigner comments:
not in my site (wp3.0.1)
because i use some plugins ?
Erez S comments:
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
Erez S comments:
BTW, where do you put the code? You have to put it inside the functions.php code, otherwise it won't work
Sébastien | French WordpressDesigner comments:
al is weel but that don't work.
have you test in wp3.0.1 ?
Erez S comments:
Yes...
Can you send me the whole theme to my email [email protected] ?
AdamGold answers:
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');
Sébastien | French WordpressDesigner comments:
don't work
AdamGold comments:
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
Sébastien | French WordpressDesigner comments:
that's an idea... but that's not really what i need...
Utkarsh Kukreti answers:
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);
}
Sébastien | French WordpressDesigner comments:
sorry, that don't work.
Have you another idea ?
Utkarsh Kukreti comments:
Had a minor typo. Please try the updated code. I've tested it on WP 3.1, and it works fine.
Sébastien | French WordpressDesigner comments:
that don't work on my site...
Utkarsh Kukreti comments:
How are you fetching the meta data in your theme / plugin?
Sébastien | French WordpressDesigner comments:
like that :
<?php echo "test : " . get_post_meta($post->ID, 'adresse1', true); ?>
Utkarsh Kukreti comments:
And you get the original value back, or nothing? Also, where did you add the filter code I wrote?
Sébastien | French WordpressDesigner comments:
OK, that work fine on another site (wp3.1) but not on the site of my client
i don't know if it's because that's wp3.0.1 or because i use plugins
Utkarsh Kukreti comments:
This filter on get_metadata was added in 3.1. There's no filter available for metadata in WP 3.0 - http://core.trac.wordpress.org/ticket/14766
Sébastien | French WordpressDesigner comments:
arf, my client use 3.0.1
Spencer answers:
As Utkarsh Kukreti said, there are no direct filters when outputting custom post meta. So, the easiest way may be to just replace your <em>get_post_meta()</em> call in the template file, with a custom function. Try putting this in <em>functions.php</em>:
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(); ?>
Just Me answers:
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?
Sébastien | French WordpressDesigner comments:
thx but i need a function :-)
Tom Ransom answers:
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
babis iosif answers:
hi there....
I am newbie and i have the same problem
I use this code and in my site working perfect
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);
}
I change only this
return str_replace('<div>this text</div>', '<span>another text</span>', $content);
to this
return str_replace('https', 'http', $content);
i want to ask, if i need one more change in custom fields like
return str_replace('aaaaaa', bbbbb', $content);
how to make this?
thanx , and sorry for my bad english