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.

Is it possible to execute the gallery shortcode before any other content no matter where the shortcode is in the post HTML?

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted
function gallery_first( $content) {
    $expr = '/\[gallery(.*?)\]/i';
    return preg_replace_callback( $expr, create_function('$matches', 'return do_shortcode($matches[0]);'), $content);
}
add_filter( 'the_content', 'gallery_first', 6); // prio 6 executes this function previous to all other filter functions
share|improve this answer
    
Thank you! I added the code to functions but for some reason it doesn't work. I added the gallery shortcode below some text but the it does not appear above. –  at least three characters Dec 17 '11 at 4:19
    
Sorry, the title of your question differs a bit from the description text. My first solution was to place the gallery at the start of the text but I modified it after reading the text of your question again. Now any gallery shortcode is simply executed before all other filters assigned to that hook. I will place the other variant as an additional answer, too. –  jot Dec 17 '11 at 4:26
    
Thank you for the code! brilliant –  at least three characters Dec 17 '11 at 19:02
add comment
function gallery_first( $content) {
    $expr = '/\[gallery(.*?)\]/i';
    return (" [gallery] " . preg_replace( $expr, '', $content)); // deletes all existing gallery shortcodes
}
add_filter( 'the_content', 'gallery_first', 6); // level '6' executes this function previous to all other filter functions
share|improve this answer
add comment

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.