This is within a POD template
Can i set a magic tag as a variable so it can be used in various PHP uses? That’s what I am struggling with, saving a magic tag as a variable so I can use it in a if statement for example.
Is this the best way to get support or is this plugin just not very well supported?
Hi Paul,
How do I get the ID when I am in a template for ‘Single Pods Item’ block? The info on your page says $books = pods( ‘books’, 123 ); but I can’t get the ID of the selected POD to use instead of 123 as I can’t set magic tags as variables.
With get_post_meta, that will give me the meta for the page I am displaying the blocks on, but again I need the POD ID to be able to get useful information, which I am unable to do.
Or am I missing something?
I assume from your answer there is noway to set a magic tag as a variable? Such as $test = {@resource_link_protected.ID}. Because these doesn’t work for PHP If statements for example.
There is do_magic_tags(): https://docs.pods.io/code/pods/do-magic-tags/ but it will likely be simplest to define a custom shortcode, or pass the ID from the magic tag to a custom function by appending a comma and the function name:
In template: {@resource_link_protected.ID,do_wpdm}
In theme functions.php or a plugin file:
<?php
function do_wpdm( $id ){
return do_shortcode( "[wpdm_package id='$id' template='link-template-button']" );
}
Thanks that works for the shortcode but I still need to save it as a variable which doesn’t seem to work
This is what I have:
In Function:
function do_getid( $id ){
return $id;
}
In File
$test = '{@resource_link_protected, do_getid}';
print_r($test);
//This shows the correct title which is Actions for Coaches Guide
while ( $wpdm_loop->have_posts() ) : $wpdm_loop->the_post();
print_r(get_the_title());
//This show the titles in my loop, one of which is Actions for Coaches Guide
if( get_the_title() == $test ){
echo 'IT WORKS!'; //This doesn't display
}
}
So setting the variable to a magic tag for some reason doesn’t work because if I was to manually change $test to $test = ‘Actions for Coaches Guide’; it does work
Magic tags are pre-parsed by PHP, so assignment can’t work that way. To save the value of a magic tag to a variable in PHP, use do_magic_tags().
Hi Paul,
Can you give me an example? There isn’t a lot of info on that page
$id = do_magic_tags( '{@resource_link_protected.ID}' );
I get an error:
Uncaught Error: Call to undefined function do_magic_tags()
Ohh I put the function in functions.php and getting some return. I will test this
function do_magic_tags( $code ){
return $code;
}
Ok so I am still having issues using in some PHP such as the If statement I put earlier. This is what I put:
$podtitle =do_magic_tags('{@resource_link_protected}');
while ( $wpdm_loop->have_posts() ) : $wpdm_loop->the_post();
if( get_the_title() == $podtitle){
echo 'IT WORKS';
}
endwhile;
I wonder if the problem exists in the encoding of the magic tages as when I var_dump $podtitle the string displays as it should but has a length of 51 while as the_title has a length of 37 even though they have the same string, so maybe some hidden characters are there?
Sorry, no, it’s a method of the pods class, not a function. My mistake. You shouldn’t need to define it.
Try this:
$pod = pods( get_post_type(), get_the_ID() );
$pdmid = $pod->field( 'resource_link_protected' ); // may or may not require “.ID”
$pdmid_mt = $pod->do_magic_tags( '{@resource_link_protected.ID}' );