Jump to content

A (probably very simple) manipulation of a WP database... help needed


richardfrank

Recommended Posts

Hi

 

It's ten years since I dabbled in php and mysql, but I have a problem with a Wordpress site that I look after for a friend which I think could be solved in five minutes of mysql... but it's far too long ago for me to remember how.

 

So...

 

I have an absolutely standard WP database and my task is to take a standard entry in post_content and extract some of it to go into post_excerpt.

 

A typical post_content looks like this:

 

[tab name=The Case]I saw this cat, with similar lesions in both eyes but comfortable and visually still, as a "Have a look at this while you are here" freebee examination in a clinic a few days ago. What are you seeing here?<a href="http://davidlwilliams.org.uk/wp-content/uploads/archivesite/pic523.jpg"><img class="aligncenter size-medium wp-image-532" title="© David Williams" src="http://davidlwilliams.org.uk/wp-content/uploads/archivesite/pic523.jpg" alt="" width="300" height="210" /></a>[/tab][tab name=David's view]This is lipid deposition with associated blood vessels probably after a corneal ulcer. We'd call this a lipid keratopathy, though I've never seen one in a cat before and can't find a report of it in the literature. Not enough money, or a calm enough cat, to take a blood sample for lipid analysis I'm afraid, but as the cat is happy no treatment is needed. If it were maybe the driver of this truck I passed on the way home from the consultation would be able to help!<a href="http://davidlwilliams.org.uk/wp-content/uploads/archivesite/pic524.jpg"><img class="aligncenter size-medium wp-image-532" title="© David Williams" src="http://davidlwilliams.org.uk/wp-content/uploads/archivesite/pic524.jpg" alt="" width="300" height="210" /></a>[/tab][end_tabset]

 

I want to get rid of the first shortcode (i.e. everything between the opening [ ] and then extract the next 20 words... add on a "..." and put it into the post_excerpt field.

 

Probably not difficult... but it is for me ten years on (and feeling decidedly middle-aged)!

 

ALSO, what's the best method for trying a solution out without potentially trashing a database?

 

Would hugely appreciate any help.

 

Richard

Link to comment
Share on other sites

You already have a field with the content, I wouldn't make another in the database or even make another query.

 

You can use post_content() with preg_match and some regex or a string replace afterwards, then limit the words.

There also exists the_excerpt() wordpress function which uses the first 55 words.

 

The best option is to make a plugin or a function in the themes function.php file.

add_filter( 'the_excerpt', 'remove_tab_excerpt' );

function remove_tab_excerpt( $excerpt ) {
    
	return preg_replace ('/\[tab[^\]]*\](.*)\[\/tab\]/', '$1', $excerpt);

}

If you want to do this just posts and not on single pages.

if(!is_single($post)) {
add_filter( 'the_excerpt', 'remove_tab_excerpt' );

function remove_tab_excerpt( $excerpt ) {
    
    return preg_replace ('/\[tab[^\]]*\](.*)\[\/tab\]/', '$1', $excerpt);

}
}

Usually when modifying wordpress is best to make a plugin or a custom theme with functions, when you update anything in wordpress can lose your changes.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.