Wordpress - referencing get_post_meta inside & outside of the loop properly? - wordpress

I've successfully created my post meta boxes, saved the data and I understand how to retrieve the data. Within the custom meta boxes I have a field for the page branding that will decide what color scheme per the product line we are featuring on that page.
I have a class per the color scheme that is triggered when the body has a class of the product line name appended to it. For example:
<body class="product-drinks">
OR
<body class="product-abcwidgets">
Depending on what product line is selected in the meta box for that post will determine what style sheet will be included.
For example if I chose "product-drinks" then the stylesheet included would be product-drinks.css.
Most of the meta box data I need to use within the loop but I also need to access the page branding mega field data outside of the loop. How would I grab this data if I need it outside of the loop?
I initially thought of placing some of the data in an array while in the loop as such and then referencing the $page_options array value in the body tag as such:
(outside of the loop in the header)
<body class="<?php echo $page_options['pageBranding'];?>
from within the loop"
$page_options = array(
'pageBranding' => get_post_meta($post_id, 'pageBranding', true),
'layout' => get_post_meta($post_id, 'pageLayout', true)
);
Am I doing this correctly or is there a better way of doing this? Or should I only reference the meta fields I need within the loop and then use global $wp_query; outside of the loop and get the post meta that way for the data I need for the body and stylesheets?

If you need the data outside the loop, I'd suggest using $wp_query or global $post.
<?php global $post; ?>
Then you can call it just as normal
<?php $samplemeta = get_post_meta($post->ID, "your_meta_name", true); ?>

Related

Wordpress: Can I use get_post_meta in my Plugin?

So I'm trying to reference custom field values in a plugin I'm building. All I need to do at this stage is grab the values and store them in variables. This is my code to get the custom field value of pageName:
<?php
global $wp_query;
$postid = $wp_query->post->ID;
$pageName = get_post_meta($postid, 'pageName', true);
wp_reset_query()
?>
So when I try to echo that out, I get nothing. I notice that my plugin runs before the head or anything else, so it's the first code in the source. My hunch is that this is due to timing and the value just isn't there yet. Is there a way to make my plugin, or this chunk of code, wait until the custom field values are there before trying to grab them?
I'm trying to avoid doing anything in the theme files so this can be a stand alone plugin that I can share.
yes, you can get the value of any post meta of the custom post type.
Just make sure that you are receiving the correct post_id in the $postid variable.
If you get the correct id of the post type you can get any meta field
Example:
global $post;
if ($post->ID) {
$media_id_meta = get_post_meta($post->ID, 'media_id', true);
}
Found the solution! I wrapped the whole thing in a function to put it in the footer, which made sure that everything it needed was there.
//----This function is wrapped around the code for my plugin
function dataLayerInject() {
*ALL MY CODE*
}
//----This drops my code into the footer
add_action('wp_footer', 'dataLayerInject');

Add WordPress filter to ACF WYSIWYG field

I am currently using the following filter to replace any instance of the string 'magic_click_link' within WordPress' the_content.
function click_link ($b) {
global $post;
$this_post_id = $post->ID;
$op_name = get_field('operator_name');
$namenospace = make_no_space("$op_name");
$tracking_link = '/go/'.$this_post_id.'/';
$click_link = '<a class="claimCTA" id="operator-step1-'.$namenospace.'" href="'.$tracking_link.'" target="_blank" rel="nofollow">Click here to go to '.$op_name.'!</a>';
$b = str_ireplace('magic_click_link',$click_link,$b);
return $b;
}
add_filter( 'the_content', 'click_link');
I have just updated this field to use an ACF WYSIWYG field, but this has caused the filter to stop working.
I believe I need to target something other than the_content inside the filter but I'm not sure what is needed...
The filter the_content automatically runs on any content output using the function the_content(), so by default, this means it only applies to standard WordPress post and page content.
To apply these filters to your custom field content as well, you need to manually call apply_filters() when you output the field's content.
For example, say your custom field is called my_extra_content. With Advanced Custom Fields, you can call:
echo apply_filters("the_content", get_field("my_extra_content"));
This will output the content of your field, while applying all the usual filters against it. Because your click_link() function is hooked to the_content already, it will therefore process your custom field content as you are desiring.

Options panel global variable?

I am creating a WordPress options panel based on
http://en.bainternet.info/2012/my-options-panel
In the article it states to call the stored options to use
//get the data to an array
$data = get_option('demo_options');
//access each field by its id
echo $data['text_field_id'];
Which works, the only problem I have is that if I want to call some information for say the header.php and the footer.php or any other page I have to include the line
$data = get_option('demo_options');
at the top of every page otherwise I can not call the data, which seems repetitive.
I have tried to create a global variable in the functions.php file like;
global $data
$data = get_option('demo_options');
but that doesn't work.
Does anyone know how I can resolve this so I don't need to add the line to the top of every page?
Thanks
In functions.php:
$data = get_option('demo_options');
In any other theme template file (header.php, single.php, page.php):
global $data;
var_dump( $data );
To have a variable defined in header.php and not having to use global $var in other template files, the following has to be done:
define the variable in header.php
instead of get_header(); in other theme template files, use include 'header.php';
this way, you can reference the variable directly without having to declare the global

Tag list Drop Down Menu

I am wondering how to create a simple tag drop-down menu (all tags included DESC) without rewriting the WP core functions. It has to work outside of any loop.
wp_tag_cloud() with the 'format=array' attribute would seem the best choice since it works outside of any loop/template and returns all available tags sorted A-Z (which I need) but the array values contain HTML formatting (instead of just a plain string value) and that is not suitable for creating the drop-down.
i.e.:
<?php $tag = wp_tag_cloud('format=array'); // 'format=array' contains <a>link</> !!!
foreach($tag as $tagkey => $tagvalue) // ...need to be somehow filtered out !!
{
echo "<option value='".$tagvalue."'>".$tagvalue."</option>";
}
?>
The get_the_tag_list() function works great but it doesn't work outside of the template (loop).
Is there a simple way how to get the list of all tags so I can put them into the drop-down?
...OMG! I can't believe I actually asked this publicly!
Of course the code is ...
<?php $tag = wp_tag_cloud('format=array' );
foreach($tag as $tagkey => $tagvalue)
{
$cleanedup = strip_tags($tagvalue);
echo "<option value='".$cleanedup."'>".$cleanedup."</option>";
}
?>

Single Page Navigation Menu Dynamically Generated

hHi all! I have posted this question on the WP support forums, but the community doesn't seem to be as active as stack's, so I am taking a chance here!
I am looking for a plugin that would automatically create a navigation menu (through the use of shortcodes for example) on a long single page documentation page.
The long page is divided into sections. I can imagine using a shortcode at the beginning of every section, and this will create a menu that would be displayed in a sidebar for example (called through a second shortcode perhaps, or a widget)
Any thoughts? Advice?
Thanks!
Use [section]Section Title[/section] shortcodes, then [section_navigation] where you want the navigation links output.
This works, but with a massive caveat -- that [section_navigation] needs to be in your post/page after the other [section] shortcodes... otherwise it generates an empty list.
You should be ok to use it in your theme by putting <?php echo do_shortcode("[section_navigation]");?> in sidebar.php. It will work as long as get_sidebar() is after the_content() in your theme templates (it usually is).
This to go in functions.php
$whit_sections = "";
// [section]My Section Title[/section]
function whit_section_shortcode( $atts, $title = null ) {
// $content is the title you have between your [section] and [/section]
$id = urlencode(strip_tags($title));
// strip_tags removes any formatting (like <em> etc) from the title.
// Then urlencode replaces spaces and so on.
global $whit_sections;
$whit_sections .= '<li>'.$title.'</li>';
return '<span id="'.$id.'">'.$title.'</span>';
}
add_shortcode('section', 'whit_section_shortcode');
// [section_navigation]
function whit_section_navigation_shortcode( $atts, $title = null ) {
global $whit_sections;
return '<ul class="section-navigation">'.$whit_sections.'</ul>';
}
add_shortcode('section_navigation', 'whit_section_navigation_shortcode');

Resources