WordPress.org

Ready to get started?Download WordPress

Forums

probleme with my plugin - str_replace (7 posts)

  1. taghaboy
    Member
    Posted 6 years ago #

    hi,
    i'm new at WP Coding, i find it cool especailly with the plugin setup, so i do creat a plugin who will to easy my need to show my archives, so i creat one who used the php "str_replace", it work perfectly whene i replace some words, but whene i put a template tag it dont
    her my code :
    ____________________
    <?php
    /*
    Plugin Name: Archives
    Plugin URI: http://agadirweb.com
    Description: Display the archives monthly or yearly.
    Version: 0.1
    Author: Hafsi Kamal
    Author URI: http://agadirweb.com
    */
    function monthly_get_archives() {
    print('
    <?php wp_get_archives('type=monthly&limit=12'); ?>
    ');
    }

    function my_function ($text) {
    $text = str_replace('###archives-monthly###', monthly_get_archives(), $text);
    return $text;
    }
    add_filter('the_content','my_function');
    ?>
    ____________________
    the word press declare an error in this lines with ***
    print('
    ***<?php wp_get_archives('type=monthly&limit=12'); ?>
    ');
    }
    thanks all for u r help

  2. Lester Chan
    Member
    Posted 6 years ago #

    because you did not escape the quotes \'

  3. taghaboy
    Member
    Posted 6 years ago #

    because you did not escape the quotes \'

    i do that but not working

  4. Lester Chan
    Member
    Posted 6 years ago #

    from what I looked at the code, you cant do that to wp_get_archives() because that function print stuffs out and not return you a value. You need to use object buffer(ob) to do it, http://php.net/ob_start

  5. taghaboy
    Member
    Posted 6 years ago #

    hello,
    really it's so hard to me to understand all thos codos, i'm not really a pro in PHP but i still learning it, if possible pls , can ypu do a touche in that code.
    thanks

  6. Lester Chan
    Member
    Posted 6 years ago #

    ob_start();
    wp_get_archives('type=monthly&limit=12');
    $archives = ob_get_contents();
    ob_end_clean();

    Do your stuff on $archives.

  7. taghaboy
    Member
    Posted 6 years ago #

    Sir,still not understand yet, where i add that code... can u to provide me the complet code please.
    thanks

Topic Closed

This topic has been closed to new replies.

About this Topic