简体   繁体   中英

Include PHP in Wordpress Shortcode

I'm trying to use this in a loop but I'm unsure how to implement it. Its a like2unlock plugin and I'm using it within a loop!

<?php echo do_shortcode('[l2g]<span class="download left"><?php get_attachment_icons($echo=true); ?></span>[/l2g]'); ?>

This code oviously wont work in my wordpress template, what are your thoughts for a resolution?

You just need to build your shortcode string:

echo do_shortcode('[l2g]<span class="download left">' . get_attachment_icons(true) . '</span>[/l2g]');

You don't need to "Include PHP" in your short code, you need to use PHP to build your shortcode.

Also, $echo = true is the notation for specifying a default parameter in a function definition. You should just pass the parameter without assigning it to a variable: get_attachment_icons(true) . Since true is the default you can also call the function without specifying any parameter.

Incidentally, get_attachment_icons($echo=true) will work correctly since an assignment evaluates to the value being assigned, but it is still not the correct way to call a function with a default parameter.

You should not be passing php into shortcode attributes. It is really bad practice and also opens up a loop hole for hackers to exploid.

The correct way is to build your php executable code into your shortcode and then using the attributes to control how your php operates within the shortcode. get_attachment_icons should be inside your shortcode. How you want to control the output can be in your attributes and passed as such

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM