I’m trying to grab all image id’s that are associated with the shortcode that are listed as exclude. For example: if my post has I’d like to get a variable that would echo like this
echo $excludes;
result 1,2,3
thank you for any help you can offer.
Leave a Reply
You must be logged in to post a comment.
The task
It’s not as easy, as it looks at a first glance. The main problem is, that you can define your own
[gallery]
shortcode and simply override the default. Actually that’s what some themes are doing (blame you ThemeForest authors!). In this case, simply hooking intopost_gallery
won’t work in every case.Drawbacks for
post_gallery
The problem is that both the shortcode, as well as the core callback can be overridden and therefore every callback will simply be knocked out.
Drawbacks for
the_content
If you’re switching to another filter and take the raw output, then you’ll have to deal (another time) with the Regex. This is slow and mostly won’t be simple.
So what to do now? Easy. Interact with the
global $shortcode_tags
. It holds the callback as second argument, so it’s actually not too hard to determine in which case we are. Then we can simply switch on demand. This way we have a fine balance between reliability and performance.Build a base plugin
Here’s a plugin that defines an
abstract
class (one that must getextend
ed in order to work). There’re three things that need definition in the child class:$part
– the part you want to retrieve$type
– the type of match you need. Valid aredigit/alphanumeric/alpha
process_atts()
– the method that processes your output – whatever you want to do with the resultJust upload this via (S)FTP into your plugins folder and activate.
Handle a task, get a child
Here you see the actual processing plugin. First it hooks itself statically to the
init
hook and then runs the parents__construct()
method to retrieve the attributes.Then you need to define what attributes you want to retrieve (see the classes properties
$part
and$type
, which already default to what you asked for).The last two decisions you need to make are
process_atts()
__construct()
and whereprocess_atts()
is hooked in.It’s as easy as that.
If you need a custom regex, just add a method named
get_regex()
to your extending class andreturn
your custom pattern. Then set the$type
to and empty string''
and you’re ready to go.Alter what you need and then again: Just upload this via (S)FTP into your plugins folder and activate.
If you also need to display the content on the same page, what about using the
post_gallery
filter? You won’t need any regex thenIt took me a while to find a solution that worked for me, but since all I was looking for was the delimited list of attachment id’s associated with a certain attribute like
exclude
orhide
, this worked for me: