WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] print only what's in the Shortcode (21 posts)

  1. Shaooxz
    Member
    Posted 4 years ago #

    hey guys,
    I'm looking for a way that i can click print on my blog and only the content that is in Shortcode will print out.
    I'm also looking for a way that what's in the shortcode will standout from the rest of the post.
    example:
    bla bla bla [print] print only this content and make the writing bold and the background color green [/print] bla bla bla

    Thanks

  2. vtxyzzy
    Member
    Posted 4 years ago #

    I think this will do what you want, but you may need to tweak it some. Put it in your functions.php, but be careful - you can disable your blog. Be sure to make a backup copy so you can ftp it back if you need to.

    <?php // [mm-hilite]some text here[/mm-hilite]
    function mm_hilite_func($atts, $content=null) {
    	extract(shortcode_atts(array(
    		'noatts' => 'Unknown',
    	), $atts));
    	$output = '';
       if (!is_null($content)) {
    		$output = "<span style='background: green; font-weight: bold;'>$content</span>";
       }
       return $output;
    }
    add_shortcode('mm-hilite', 'mm_hilite_func');
    ?>
  3. Shaooxz
    Member
    Posted 4 years ago #

    this is grat vtxyzzy, but it covers only the highlighting part. how can i make th WP-print plugin (or any other print plugin) to print ONLY what's in [mm-hilite]some text here[/mm-hilite] ?

  4. vtxyzzy
    Member
    Posted 4 years ago #

    I think I misunderstood what you wanted. Did you want to not print anything outside the markers, but only the text between with nothing else?

  5. Shaooxz
    Member
    Posted 4 years ago #

    i did but now i have this problem: http://wordpress.org/support/topic/343941?replies=1

    i don't know if it has nothing to do with it or its from the 2.9 upgrade. Any way i copied the original file but i still have that problem.. btw u did mean the functions.php in the theme right?

  6. vtxyzzy
    Member
    Posted 4 years ago #

    Yes, I meant the functions.php in the theme. However, there is a basic problem with using shortcodes to affect text OUTSIDE the markers. That text is not passed to the shortcode function, so it cannot be suppressed as far as I know.

    To do that would require you to write your own function for the_content filter. The function would scan for your markers and return only the text between them, probably using a regular expression.

    That is beyond what I can do in this forum - sorry.

  7. Shaooxz
    Member
    Posted 4 years ago #

    "Did you want to not print anything outside the markers, but only the text between with nothing else? "

    yes, that's exactly what i wanted. Your code works great for the highlighting part but when i try to print i still see all of the post.
    Any other recommendations guys?

  8. vtxyzzy
    Member
    Posted 4 years ago #

    It might be possible to put the text you want to print inside a special div tag and then provide a print.css file to hide the rest of the text - just a thought.

  9. Shaooxz
    Member
    Posted 4 years ago #

    i dont know how101cookbooks do it but that's a great example for what im looking for.

    example: http://www.101cookbooks.com/archives/sparkling-cranberries-recipe.html

  10. Shaooxz
    Member
    Posted 4 years ago #

    vtxyzzy, i have another question. how can i use the code you gave me but i want the style to be in my style.css?
    something like

  11. vtxyzzy
    Member
    Posted 4 years ago #

    Inside the span tag, put a class name instead of the style. Then put the style for that class in your style.css.

    Where on the 101Cookbooks site is the example of what you want?

  12. Shaooxz
    Member
    Posted 4 years ago #

    something like that?

    $output = "<span recipe='background: green; font-weight: bold;'>$content</span>";

    css:

    #recipe {
    background-color:#F7F7E7;
    }
  13. vtxyzzy
    Member
    Posted 4 years ago #

    Like this

    $output = "<span class='recipe';>$content</span>"

    css:

    .recipe {
       background-color: #F7F7E7;
    }
  14. Shaooxz
    Member
    Posted 4 years ago #

    thanks vtxyzzy for your help!
    its working.. kinda
    if im using margin-right:50px; in my css only the first line will move, not all the writing in the shortcode. if i'll use background-color in my css i won't get a square block of color but only where there's writing will get background color. i have no idea why the css acts like that now

  15. vtxyzzy
    Member
    Posted 4 years ago #

    Can you put the writing in a div or p tag instead of a span, or will that mess up the surrounding text?

  16. Shaooxz
    Member
    Posted 4 years ago #

    it will create a mess. the idea was to do it once and after that just to use shortcode in my post without writing divs every post

  17. vtxyzzy
    Member
    Posted 4 years ago #

    No, I mean use a div in the shortcode function instead of the span.

    $output = "<div class='recipe';>$content</div>"

  18. Shaooxz
    Member
    Posted 4 years ago #

    great! thanks a lot vtxyzzy.
    i also managed not to print what i don't want to with the shortcode [/donotprint] that aperently implented in the wp-print plugin. However, im trying to get the printing link inside of the [recipe]...[/recipe]
    but if i use [print_link] in the post i just see the txt [print_link].
    I can't use any [shorcode] inside of the recipe ones. and idea how to bypass that?

  19. vtxyzzy
    Member
    Posted 4 years ago #

    Sorry - no idea at all!

  20. Shaooxz
    Member
    Posted 4 years ago #

    thanks again man, you've been a great help!

  21. vtxyzzy
    Member
    Posted 4 years ago #

    You're welcome. Please mark this topic 'Resolved'.

Topic Closed

This topic has been closed to new replies.

About this Topic