WordPress.org

Ready to get started?Download WordPress

Forums

Search and replace words with code (5 posts)

  1. monocultured
    Member
    Posted 6 years ago #

    Here's what I want to accomplish:

    wp_replace.png

    Baisically, I want a function that will scan through the text in a post, and at the first instance of a specific word on any given page, it will display a small side-text (that's the small text on the right in the image). The list contains some fifty odd words with descriptions, all contained in a textfile.

    I know that I need a plugin in order to force wp to push code into a post (as opposed to only allowing certain tags), but I'm curious if I should be using php for this or if javascript is better suited.

    I guess this question could be divided into two parts:

    1 - How do I add a description of specific words dynamically based on a seperate list of words?
    2 - Is it possible to style the text to appear with the top aligned with the word?

    There will be trouble if too many words trigger the funtion in any given post, but that'll be a later concern, and hopefully fixable through css (or php->css)

    If anyone could let me know if this is at all worth the hassle, and which method I should consider, I would appreciate it greatly.

  2. Sridhar Katakam
    Member
    Posted 6 years ago #

  3. monocultured
    Member
    Posted 6 years ago #

    Thanks a lot srikat, I appreciate the links.

    The alinks plugin code is too dense for me to understand and apply on a solution of my own, but the Youtube video send me on the right track.

    Here's what I've ended up with (a function that replaces the word "stil" with some divs and paragraphs):

    <?php
    
    /*
    Plugin Name: Replace word
    */
    
    function replace ($text) {
    	$text = str_replace('stil', '<div class="definition_word"><p class="word">stil
    </div><div class="definition"><p class="ord">stil:
    En benämning på vad du än gillar, och dessutom en apelsin.
    </div>', $text);
    	return $text;
    }
    
    add_filter('the_content', 'replace'); ?>

    It gives me a DIV that CSS positions absolute to the right, and it looks like I might get this to work.

    Two things that I don't understand though:

    1) The wordlist contains some sixty words with definitions, and although I could, using this method, create one plugin for each definition I'd rather not; in other words, how do I add more than one word for the plugin to search for?

    2) What would be the best way of making sure that the plugin only replaces the first occurance of a word on any given page, and not the subsequent ones?

    I know that what I've come up with isn't elegant, but I'm more of a brute force type of person, and it can very well be ugly as long as it works.

    Any help would be appreciated.

  4. monocultured
    Member
    Posted 6 years ago #

    Ah, looking at php.net/str_replace i could use an array with different words to look for and their replacement. Barring interfacing with an external text-file db, would this be the most convenient way of doing this?

  5. zglows
    Member
    Posted 6 years ago #

    thanks for this!

Topic Closed

This topic has been closed to new replies.

About this Topic