• googlebot

    (@googlebot)


    I’m just scratching the topic, learning php and how WP plugins are coded.

    I need some help to understand a couple of things.

    My first plugin is very very basic, preg_replace to change whatever is wrapped in <replace_this> in a post, so far it works, now I’m trying to add stuff inside the post.

    My first functions is:

    function my_replace($test) {
      $needed = "test"; // actually it should be the function my_func()
      $test = preg_replace('/<replace_this>(.*?)<\/replace_this>/i',$needed,$test);
       return $test;
    
    }
    
    add_filter('the_content', 'my_replace');

    the other functions is:

    function my_func() {
      $container_open = "<div style='border:1px solid #000; width:380px; margin:4px auto; background:#bbaa99;padding:4px;'>"; //ugly way to do it
      $container_close = "</div>"; // even uglier ah ah
    
      $myarray = array("cool","ok","awesome","great","meh");
    		sort ($myarray);
    		for ($i=0; $i<count($myarray); $i++){
    		echo $container_open.$i." is ".$myarray[$i]."".$container_close;
    
    	}		
    
    }return $content;
    
    add_filter('the_content', 'my_func');

    So, $needed should output the array of the second function but I don’t know how to do it.
    I even tried to do something like $needed = “<?php my_func(); ?>”; and use the plugin Exec-PHP to run that code inside the post but doesn’t work.

    I know I’m doing it wrong, but right now I’m trying to get into the logic of programming and can’t think of another way (that I’m aware of).

    Any input is appreciated, thanks.

Viewing 1 replies (of 1 total)
  • Kalessin

    (@kalessin)

    You’re trying to run before you can walk.

    Don’t echo text, add it to the $content variable that you’re trying to return at the end of the function. Oh, and return the $content variable before you close the braces.

    In your function my_replace(), why not set a variable to the result of my_func() and replace your found occurrences of <replace_this> with that variable?

    Since you are a beginner, I would recommend getting this script to work on a standalone basis, then turning it into a WordPress plugin. Otherwise, how can you possibly work out what you’re doing wrong?

Viewing 1 replies (of 1 total)
  • The topic ‘help writing plugin’ is closed to new replies.