WordPress.org

Ready to get started?Download WordPress

Forums

Replacing a Post based on regex (4 posts)

  1. mouseclone
    Member
    Posted 6 years ago #

    Ok what I'm trying to do is kind of weired and I'm not the best with PHP/WP hooks.

    I created a plug in for a meme that I manage, and the blog is http://amiest.musicwednesday.com. That way you can look and see what I did with the current plugin if you would like.

    I'm not sure that I'm doing this correcty, but what I'm trying to get done is in this order:

    1) Get post
    2) place information before and after post.
    ie. $post = 'some data here' . $post . 'some more data here';
    3) push the $post with the new information back into the database.

    I would like this to happen when it is published. I have tried save_post and publish_post with no luck at all.

    I also know that I have to do a preg_match so that it will know what post to apply the plugin to. The regex needs to be dynamic numbers like ([0-9]*). I was looking at something like $dyno = '/(\[([0-9]*)\])/'; this way it would match [4] or [48374]. I am not even sure that this is correct.

    I hope that I have provided enough information for some help to get me pointed in the right direction. Thank you in advance.

  2. alakhnor
    Member
    Posted 6 years ago #

    Try 'wp_insert_post'.

  3. mouseclone
    Member
    Posted 6 years ago #

    Ok I'm still not getting it.

    Currently I can replace [test] in a post with this function:

    function asmw($text) {
            $asmw_tag_pat = '/(\[test\])/';
            $test = 'This is the text to replace';
    
            if (preg_match($asmw_tag_pat, $text, $matches)) {
                    $text = preg_replace($asmw_tag_pat, $test, $text);
            }
    
    return $text;
    }
    
    add_filter('content_save_pre', 'asmw');

    This is currently the way that the plugin works now and it works well. The problem is that it will only replace [test].

    is there an easy way to wrap a post in a table or div tags before it goes into the database?

  4. johnkolbert
    Member
    Posted 6 years ago #

    I guess I dont' get what the problem is. What's wrong with this:

    function asmw($content){
         $content = '<div id="post">' . $content . '</div>';
    
         return $content;
    }
    
    add_filter('content_save_pre', asmw);

Topic Closed

This topic has been closed to new replies.

About this Topic