Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I write posts about math and computer science. Many of those posts need some previous knowledge. I would like to create a box for some articles with links to these basics, simmilar to UbuntuUsers. In my articles I would like to add them like this

{{previousKnowledge|<a href="...">first</a>|<a href="...">second</a>}}

Does a plugin exist for this task?

Can I create a custom template which does basically replace this text by the following?

<div id="previousKnowledge">
  <ul>
    <li><a href="" id="source-1">first</a></li>
    <li><a href="" id="source-2">second</a></li>
  </ul>
</div>
share|improve this question

2 Answers 2

up vote 1 down vote accepted

First of all : Cool idea. I don't know whether there exists a plugin for this task, but I would simply implement this as a new small plugin.

I would recommend changing your syntax, so that it won't contain the links as html.

I changed it to:

{{previousKnowledge|first=...|second=...}}

Here is my simple first approach :

<?php
    /*
     Plugin Name: Previous Knowledge
    Plugin URI: TODO
    Description: TODO
    Author: xaeDes
    Version: 0.1
    Author URI: TODO
    License: GPL2
    */


    function previous_knowledge_preg_replace_callback( $matches ) {
        //$matches[1] contains the '...' in '{{previousKnowledge|...}}'
        $items = explode("|", $matches[1]);
        $replaced = '<div id="previousKnowledge"><ul>';
        foreach( $items as $item ) {
            $item_info = explode("=",$item);
            $replaced .= "<li><a href='${item_info[1]}'>${item_info[0]}</a></li>";
        }
        $replaced .= '</ul></div>';
        return $replaced;
    }

    /**
     * Looks for occurrences of {{previousKnowledge|...}} tags and replaces them by html.
     * @param string $content
     * @return string
     */
    function previous_knowledge_content_filter( $content ) {
        return preg_replace_callback('#{{previousKnowledge\\s*\\|(.+)}}#i','previous_knowledge_preg_replace_callback', $content);
    }

    add_filter( 'the_content' , 'previous_knowledge_content_filter' );
?>

I tested with your example and it works.

You may change the output html in previous_knowledge_preg_replace_callback(..).

How to install it in your wordpress :

  1. Create a folder named 'previous_knowledge' (or whatever name you find appropriate) in wp-contents/plugins/.

  2. Create a php file named previous_knowledge.php inside this folder and paste the code into the file.

  3. Now you only have to activate it and put "{{previousKnowledge|first=...|second=...}}" into your posts or pages.

Do you want further explanations?

share|improve this answer
    
Hi xaedes, this looks quite good! I have to test it on an offline version of my blog. If it works, I'll accept your answer. At the moment I just gave you +1. –  moose Mar 24 '12 at 21:09

Maybe some thing like this:

function previousKnowledge_shortcode( $atts ) {
extract( shortcode_atts( array(
    'urls' => '',
    'titles' => ''
), $atts ) );

$url = explode("|", $urls);
$title = explode("|", $titles);
$c = count($url) - 1;
$code = '<div id="previousKnowledge"><ul>';
for ($i = 0; $i <= $c; $i++) {
    $b = $i +1;
    $code .= '<li><a href="'.$url[$i].'" id="source-'.$b.'">'.$title[$i].'</a></li>';
}
$code .= '</ul></div>';

return $code;
}

add_shortcode( 'previousKnowledge', 'previousKnowledge_shortcode' );

the short code will be: [previousKnowledge urls="http://www.site.com|http://www.site2.com|http://www.site3.com" titles="site1|site2|site3"]

and it will preduce the HTML:

<div id="previousKnowledge">
  <ul>
    <li><a href="http://www.site.com" id="source-1">site1</a></li>
    <li><a href="http://www.site2.com" id="source-2">site2</a></li>
    <li><a href="http://www.site3.com" id="source-3">site3</a></li>
  </ul>
</div>

NOTICE: I've tested it, so it works :). Let me know if it helps.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.