Ask your WordPress questions! Pay money and get answers fast! (more info)

populate the_content with the content of a field WordPress

  • SOLVED

i don't use the default visual editor of wp for page & post but i use several custom fields.
But for the plugin of seo i use, i need to populate $post->post_content with the content of several fields (a field 'name', a field 'description' and a field 'products' for example).
How can i do to populate automaticly $post->post_content with the content of this fields ?

Thanks

Answers (1)

2012-10-26

John Cotton answers:

Do you actually want to store that info in the database twice? ie in the custom field AND in the post_content table?

I would have thought not, so how about adding it to the content on the fly:


function my_the_content_filter($content) {
global $post;

$meta = get_post_meta( $post->ID );

$fields = array( 'name', 'description', 'products' );

foreach( $fields as $field ) {
$content .= "<p><span>$field:</span> {$meta[$field][0]}</p>';
}

return $content;
}
add_filter( 'the_content', 'my_the_content_filter' );


If you really wanted to save the data (perhaps in case sometime you access post_content directly), then you might be able to use the "save_post" hook to insert the data. But that could get messy since you couldn't use wp_update_post directly as that would trigger the hook again (which would trigger the save, which would trigger the hook....you get the idea).

So you'd need to write you own insert. Easy enough, but why do it if you can always filter the output and insert your values there?


Sébastien | French WordpressDesigner comments:

my seo plugin must "think" that the content of my page is "the content of custom field NAME + the content of custom field PRODUCT etc..."

I don't understand how i must use your code to do that.


John Cotton comments:

<blockquote>my seo plugin must "think" that the content of my page is "the content of custom field NAME + the content of custom field PRODUCT etc..."</blockquote>
You need to look at the SEO plugin code and see whether it ever retrieves the post content directly (eg by doing $post = get_post($id); echo $post->post_content).

If it does, then you need to store whatever you want it to see in the database.

If, however, it always using the_content() or get_the_content(), then just add my code to your functions.php (changing it to return prescisely the words you want) and it "will always see" that!


Sébastien | French WordpressDesigner comments:

i try your code but that doesn't work, it seems.
The content is not "seen" by the seo plugin
and I don't see the content of my field populating the visual editor.


Sébastien | French WordpressDesigner comments:

how can i verify that your code works ?


Sébastien | French WordpressDesigner comments:

for each post/page, my seo plugin add 3 fields "meta title", "meta description", "meta keywords".
Maybe is it possible to populate each of this 3 fields with the content of another fields (my custom fields).
Is it more easy ?


John Cotton comments:

<blockquote>how can i verify that your code works ?</blockquote>

Try adding $content .= 'sebastian'; just before the return $content;.

If you see Sebastian, then the access to the meta data is incorrect (clearly you need to use the actual field names you have - I've just guessed from what you said).

If you don't see sebastian, then the filter isn't being applied. You need to check that the file where that code is is being loaded.


John Cotton comments:

<blockquote>
for each post/page, my seo plugin add 3 fields "meta title", "meta description", "meta keywords".
Maybe is it possible to populate each of this 3 fields with the content of another fields (my custom fields).
Is it more easy ?
</blockquote>

Well, if you tell the plugin what you want directly then of course it's easier.

I don't really understand what you are trying to do. Get it to produce SEO data on the basis of meta data?


Sébastien | French WordpressDesigner comments:

i add $content .= 'sebastien'; but i don't see "sebastien" nowhere.

Ok i try to explain to you what i want (sorry for my poor english language)

I have used wpSEO plugin for long time. Now i use magic fields too.
But i see that wpseo don't create the META for the page and post using custom fields (cteated by magic fields plugin).

When i use magic fields in a page or a post i don't use the default visual editor. But wpSEO need a $post->post_content to generate the META.

So i need to have a post content.

That's my question.


But now, i think that is more easy and more handy for me (for different reasons) if you can explain to me how i can populate the fields "description", "title", "keyword" created by WPSEO i f they are empty).


I need do the same thing for several published posts.

Tell me if you have understand and tell me if you can help me.
And tell me if i must to increase my fee.



John Cotton comments:

Can you PM a login to your site? It might be quicker..