Options panel global variable? - wordpress

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

Related

Get current page id and page title inside the theme functions.php

Is it possible to get the current page id and page title inside the current themes functions.php?.
Note: I'm not referring post id.
I have tried following functions inside the themes->twentytwentytwo->functions.php file and did not work.
prin_r(the_title());
print_r(get_queried_object_id());
print_r(get_the_title());
From what I understand functions.php is included too early to use $post or get_queried_object_id().
What I've done before is create a function in functions.php that you can call in your template and pass it the id or the title.
For instance in your functions.php:
function myPage($pageId, $pageTitle) {
//Do whatever you want and return the result
}
Then in your template just call the function:
<?php
$myPage(get_the_ID(), get_the_title());
?>
Hope it helps

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');

is wrong to add my code in wp admin page wordpress

I must to add new options and functions in post pages in admin panel. I call a new function in edit-form-advanced.php and edded this function in template.php file. The question is this wrong? Becouse my function is in one file with functions on wordpress. Or maybe must be in other file? but where i must call it?
For wp-content part i know and i make a child theme of parent theme, but i do not know what to do when i must add code in wp-admin part.
example:
edit-form-advanced.php
do_custom_boxes( null, $post );
and in template.php
function do_custom_boxes( $screen, $object ) {
global $wpdb;
$appTable = $wpdb->prefix . "post_panel";
$query = $wpdb->prepare("SELECT * FROM $appTable WHERE post_id = ".$_GET['post']." ", $screen);
$applications = $wpdb->get_results($query);
......
}
Short answer: Yes, it's wrong to do so. Whenever you update your WordPress you'll loose all your changes.
WordPress allows you to hook into its code, modify its behavior and many things.
Please read about actions and filters.
Basically, Actions allow you to fire a function when something happens in WordPress.
For example:
<?php
function do_something_when_admin_pages_init() {
// Do something here
}
add_action('admin_init', 'do_something_when_admin_pages_init')
Filters allow you to modify data/output of another function. It's like it let you step in the middle, do something with the data and then continue.
Example from the WordPress page:
<?php
function wporg_filter_title($title) {
return 'The ' . $title . ' was filtered';
}
add_filter('the_title', 'wporg_filter_title');
This modifies the title before it's printed.
So with those two ways of 'hooking' into the WordPress code, you can write your code in your theme's functions.php file, or write a Plugin (it's up to you).

WordPress, pass variable from page.php to header.php

I try to create a theme in WordPress, and allow the user for some Page Templates, to load either a Slide-show in the header or to display the title.
Lets say, I have a template name called, Portfolio and another page template called Portfolio with Slide-show In Head.
Can I from within the portfolio.php and portfolio-with-slide.php to send variables in the header.php in order to decide what to display, or have I to create a second header for the second option and load the one need it into the template file with get_header('title') and get_header('slide')
What is the best approach ?
I personally use the second option - create a second header for the second option and load the one need it into the template file with get_header('title') and get_header('slide').
This is the best approach in terms of code maintainability.
A proper solution is to write a filter to replace the title:
function this_is_the_title_now( $title ) {
// can return un-altered $title or can use fancy logic here
return( "This is the new title." );
}
add_filter( 'the_title', 'this_is_the_title_now', 10, 2 );
This can be put into functions.php of your theme, or in page-whatever.php.
since wordpress 5.5 get_header has $args parameter ready to use:
https://developer.wordpress.org/reference/functions/get_header/
You can just put your arguments into get_header like this:
get_header( 'yourheadername', [ 'header_arg' => 'XYZ' ] ); (if you're using customized header.php file, in this case would it be: header-yourheadername.php)
or for default header.php file:
get_header( null, [ 'header_arg' => 'XYZ' ] );
then inside your header file you can use:
<?php echo $args['header_arg']; ?>
inside if or whatever you want :-)
You can use set_query_var to send variables to your header.php file, or any template part file.
For example, in your Portfolio Slideshow template (portfolio-with-slide.php) file:
//portfolio-with-slide.php
set_query_var('includeSlideshow', true);
Then in your header.php file (or a template part file):
//Header.php
$slideshowHeader = get_query_var('includeSlideShow');
//If variable exists and is boolean true
if($slideshowHeader !== null && $slideshowHeader === true) {
//code to include slideshow in header
}

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

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); ?>

Resources