WordPress.org

Ready to get started?Download WordPress

Forums

Help with my first plugin (4 posts)

  1. Jake232
    Member
    Posted 4 years ago #

    Hi guys

    Im hoping to start developing wordpress plugins to expand my knowledge of PHP while also making plugins to help people out.

    This is my first plugin, so I thought i would go with something Extremely simple, or so I thought it would be. Basicly, when you activate this plugin, and goto the plugin menu in the admin panel, you can select a word you wish to replace, and then the word you wish to replace it with, and the plugin will do this for you, if you wish to change back - you can simply disable the plugin.

    As far as I can tell, in theory the following code should work, however it is not. I was wondering if some-body could explain why not?

    <?php
    /*
    Plugin name: Jake's plugin
    Plugin Description: This is a plugin which when activated, will replace a word with another word..
    Version: 1.00
    Author: Jake Austwick
    Author URI: http://www.game-tec.com
    */
    ?>
    <?php
    
     function test_nav() {
      //add_options_page(Title,Menu title,Access Level,File,Function)
    	add_options_page("test","test",1,"plugin.php","test_admin");
    }
    
    add_action("admin_menu","test_nav");
    
    function test_admin() {
    	echo '
    <form method="post" action="'.$_SERVER['PHP_SELF'].'">
    Word:
    <input type="text" name="word" />
    <br />
    Replace with:
    <input type="text" name="replacewith" /><br />
    <input type="submit" />
    </form>
    ';}
    
    if($_POST["word"] && $_POST["replacewith"]){
    
    function replace( $content ) {
        $word = $_POST["word"];
        $replace = $_POST["replacewith"];
    	$content = str_replace($word,$replace, $content);
    	return $content;
    }
    add_filter("the_content",replace);
    
    }
    ?>
  2. Jake232
    Member
    Posted 4 years ago #

    SO nobody has the answer to this? I thought it would be an easy question for anyone experienced :S

  3. Jake232
    Member
    Posted 4 years ago #

    I cant believe nobody knows this? Have I asked in the wrong section or something?

  4. pbflash
    Member
    Posted 4 years ago #

    It is not working because the content filter is used when viewing individual posts/pages. It is not going to apply to every post/page when submitting a form from the admin area.

    The form in the admin area should save the word and replacewith to a db table or text file. Then the replace code could read from the table or file and replace the word when a post/page is being viewed.

Topic Closed

This topic has been closed to new replies.

About this Topic