0

I want to look for all strings in my theme located between my enclosing shortcode [question] desired string [/question].

Then I want to output the so collected strings somewhere in my theme , e.g. listing all those questions and linking them each to their containing post.

The idea behind is that I am building a wordpress blog where I write down all the things i learned in blog posts. basically so far for myself. When I have questions about a certain topic I wanna note them down in the blog post between those tags. To find and answer the questions afterwards I want to use this filter function.

So far I was able to get one string between the shortcode using the Options API and this code. But it only lets me save one question at a time while I want to store all strings that are located between [question][/question] shortcodes. Floemi suggested using the Settings API. I am trying to get my head around but there might be a more semantic solution to this.

 function question_shortcode($atts, $content = null ) {
    global $question_shortcode_text;
    $question_shortcode_text = $content;
    echo "<h2 class='question'>" . $question_shortcode_text . "</h2>";
    update_option( 'question_shortcode_option', $question_shortcode_text );
};
add_action( 'save_post', 'question_shortcode' );

add_filter( 'the_content', 'testing_var');
function testing_var( $content ) {
    $question_from_shortcode = get_option( 'question_shortcode_option' );
    $content = $question_from_shortcode . $content; 
    return $content;
}

add_shortcode( 'question', 'question_shortcode' );
  • Why don´t you use hyperlinks? – flomei Nov 26 '15 at 13:50
  • then how would you call all the hyperlinks from the blog posts in a list on a static page ( for example ) ? – MrKainig Nov 26 '15 at 13:59
  • Maybe its a misunderstanding, my idea was that i write something between those tags inside of the blog post and the question list should be generated automatically somewhere else. – MrKainig Nov 26 '15 at 14:05
  • I am trying to get my head around preg_match now and try to use that – MrKainig Nov 26 '15 at 14:12
0

Basic idea of how this could be done:

  • Hook into save_post or something else which is regularly run, but only by you (could get resource intensive otherwise)
  • Parse your content and have a look for your shortcodes. The Shortcode API could come in handy here.
  • If you found your shortcode, save the text between your shortcode and the Post ID, probably by using the Options API the Settings API
  • Provide a special template/shortcode on your overview page, which reads those options and uses get_permalink() to build links to your pages

This should help you for further researching.

  • thank you! I am gonna drop the preg_match idea for now, this looks like a more promising approach, gonna give an update soon – MrKainig Nov 26 '15 at 14:16
  • Can you help me with the part of saving the text with the Options API? I can't get it working... – MrKainig Nov 26 '15 at 15:33
  • Seems like the Options API is not the right way to go, it´s for singular entries. Settings API could be more useful. Look here for some information on that. – flomei Nov 26 '15 at 16:29
  • Hey flomei I just edited the post and yes I think I am encountering the issue you are talking about - I am only able to create and retrieve a single entry. Gonna have a look at the settings API and your other link now – MrKainig Nov 26 '15 at 16:33
  • 1
    I will think about it after work. Probably can help you with some code then. – flomei Nov 26 '15 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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