I try to make shortcode plugin which display number of my shortcodes.
<?php
$numberOfShortcodes = 0;
function ppndr_shortcode( $atts ){
$numberOfShortcodes++;
return $numberOfShortcodes;
}
add_shortcode( 'countdown', 'ppndr_shortcode' );
?>
When i add two shortcodes it display "11". How to increase numberOfShortcodes?
Use global keyword:
<?php
$numberOfShortcodes = 0;
function ppndr_shortcode( $atts ){
global $numberOfShortcodes;
$numberOfShortcodes++;
return $numberOfShortcodes;
}
add_shortcode( 'countdown', 'ppndr_shortcode' );
?>
Why? You are trying to get value of global variable $numberOfShortcodes, but server thinks you want the local (from function scope), which is not set, so creates new one. If you use global, php will know, you mean the global variable and will use it.
Related
I'm trying to store a Wordpress query results, that return infos from about 2000 posts, to use that infos after an ajax request.
In this way the query is executed only one time at page load and not every time the ajax call is request.
Here a simple code, with a simple variable, to explain what i'm trying to do inside function.php
global $myGlobalVar;
$myGlobalVar ='Hello World';
function my_function() {
global $myGlobalVar;
if(is_page_template('template-summer.php')){
$myGlobalVar = get_post_type_object( 'summer' );
$myGlobalVar = $myGlobalVar->name;
} else {
$myGlobalVar = get_post_type_object( 'winter' );
$myGlobalVar = $myGlobalVar->name;
}
}
add_action( 'wp_head', 'my_function');
Inside Page template, and ajax php template, i just print the variable in this way
global $myGlobalVar;
echo $myGlobalVar;
Code return the right global variable when page is loaded (Summer for summer template page and Winter for other templates).
But, when i try to print the variale after ajax request, the variable return "Hello World" value.
How i can make this work, without using transient or cache?
Thanks
I am trying to call function, created with add_action using do_action:
In the function.php of the theme:
function bleute_utbi_service_type()
{
return "service";
}
add_action('utbi_service_type', 'bleute_utbi_service_type');
Now, I need to get the value of the function in a plugin file:
// 'x' plugin file:
function get_valor(){
$val = do_action('utbi_service_type');
echo "this is the valor:" . $val";
}
This way of doing is not working, $val return 'null'...why?
Action hooks do not return content, and honestly if you need an action hook to return content there is a pretty good chance that you are doing something wrong.
add_action() takes the identification of a function, in your case
bleute_utbi_service_type(), and puts it into a list of functions to be called
whenever anybody calls do_action().
Either use $params with your do_action and add_action and then set the $value in your add_action callback function or use filters to return contents. To know how return works with filters, u may refer here: Wordpress: How to return value when use add_filter? or https://developer.wordpress.org/reference/functions/add_filter/
If you want to do it with add_action than you have to follow this referenece by passing argument to add_action
Reference
else try using apply filters like this.
add above code in function.php:
function example_callback( $string ) {
return $string;
}
add_filter( 'example_filter', 'example_callback', 10, 1 );
function get_valor(){
$val = apply_filters( 'example_filter', 'filter me' );
echo "this is the valor:". $val;
}
and add below code to wherever you want to print:
get_valor();
hope it works for you :)
public function action_save_post($post_id) {
global $wpdb;
$wooqty = $_POST['qty'];
if( !empty( $wooqty ) )
update_post_meta( $post_id, 'qty', esc_attr( $wooqty ) );
}
This will save the data from a Form field in Metabox in wordpess. I would like to use this variable $wooqty which has a value to another function.
public function action_woocommerce_add_order_item_meta($item_id, $values) {
global $wpdb;
$product_no = (int) $values['product_id'];
$qty_no = (int) $values['quantity'];
}
I would like to replace the variable $qty_no data to the $wooqty. my problem is how do i pass the variable $wooqty to this function so that i can replace the values of $qty_no.
Seems simple enough to me. You don't need to set a global, since it is already in Wordpress post meta data. Just call get_post_meta($product_no, 'qty', TRUE); within the function.
So, your new function will look like this:
public function action_woocommerce_add_order_item_meta($item_id, $values) {
global $wpdb;
$product_no = (int) $values['product_id'];
$qty_no = get_post_meta($product_no, 'qty', TRUE);
}
Not sure why you are globalizing $wpdb, since the function doesn't do any database operations using that variable. You should be fine with removing the global $wpdb; definition at the top of your function as well.
Furthermore, I would check $item_id variable and see if that is equal to $values['product_id'], and if it is, use that instead of refetching the product id from the $values array.
Hope I helped, if not be clearer in your question. Question seems a bit old, so you probably already got this figured out by now. But just in case you haven't.
so you can use the following syntax to set the global from within a function and then access it elsewhere in PHP.
function foo(){
$GLOBALS['foobar'] = 'somestring';
}
function bar(){
echo $GLOBALS['foobar'];
}
foo();
bar();
However, it's not often a good idea and there may be a better way to pass the variable to the functions. How about a third function that sets the variable locally that's called from within functions 1 and 2 for instance.
I'm designing a website in Wordpress. This site is one of those parallax sites where all the pages are printed on the homepage and the menu scrolls to the anchors.
That being said I am using a wp_query to pull out all the pages that are in the main menu. Furthermore I have a shortcode that I use in the content that also requires the use of wp_query.
The problem I have is that the shortcode (the embedded wp_query) is screwing up the postdata. I know when using wp_query you'd usually want to use wp_reset_postdata but in this particular situation it doesn't work because this function call will restore the postdata of the homepage and not of the currently running wp_query (sorry if I'm being unclear).
Is there a way to take a snapshot of the postdata to then restore after my shortcode? I'm looking for something along the lines of:
function my_shortcode() {
save_postdata(); //saves the current postdata
$query = new WP_Query();
while( $query->have_posts() ) {
$query->the_post();
echo get_the_title();
}
my_wp_reset_postdata(); //restores the postdata to where it was before the loop
}
By looking in the source for wp_reset_query(), you will see that what it does is that it simply restores the $wp_query global variable from another global variable($wp_the_query - this is set-up together with the initial set-up for $wp_query, so it holds the original query).
What you can do is you can simply assign $wp_query to a different global variable and then later restore it. Here's an example:
function _save_query( $var = '_wp_query' ) {
$GLOBALS[ $var ] = $GLOBALS['wp_query'];
}
function _wp_reset_query( $var = '_wp_query' ) {
$GLOBALS['wp_query'] = $GLOBALS[ $var ];
wp_reset_postdata();
}
So simply call _save_query() before overwriting the query(you can pass a custom variable name - this way you can store multiple WP_Query objects :) ).
Once you want to restore the query data, call _wp_reset_query() - again you can pass a string as a variable name in order to restore this exact query object.
This is how I managed to get it working, credit goes to Nikola's question since I worked off of his idea.
function _save_query( $var = '_wp_query' ) {
global $post;
$GLOBALS[ $var ] = $post;
}
function _wp_reset_query( $var = '_wp_query' ) {
global $post;
$post = $GLOBALS[ $var ];
setup_postdata( $post );
}
I looked at the documentation of how the loop works found here. I decided to use the same kind of setup as in Nikola's answer since it met my criteria but I used the implementation of the_post to restore the postdata. This is probably not very efficient since it's using the setup_postdata function (which I assume is overkill) but it has definitely solved my problem.
So now when I embed a wp_query I can just do the following:
_save_query();
$products = new WP_Query( $args );
if( $products->have_posts() ) {
$ob .= '<ul class="group-posts">';
while ( $products->have_posts() ) {
$products->the_post();
$ob .= '<li>'.get_the_title().'</li>';
}
_wp_reset_query();
$ob .= '</ul>';
}
Side question/note: What's the etiquette for marking an answer as the correct answer? I'd feel bad accepting my answer as the correct one when Nikola helped me reach it?
my_wp_reset_postdatadoes not exist. You have to use wp_reset_postdata(). But in a situation where you have to chain multiple wp_queries and come back to the older ones, you can store your first query in a variable, set the new WP_query, then reset it and come back to the old one.
$wp_query stores the current loop. So you can go something like :
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query($args);
...
// then later
$wp_query = $temp;
// And back on tracks !
I have this function:
<?php myWidget() { ?>
<div id="<?php echo $args['widget_id']; ?>">
<p>Something</p>
</div>
<?php } ?>
(widget_id is Wordpress core function so I have no direct access to that, it just simply generates widgets name)
I wanted to add my widget as a shortcode so needed to create another function:
function myWidget_shortcode( $atts ) {
extract( shortcode_atts( array(
/* attributes here */
), $atts ) );
ob_start();
the_widget(myWidget);
return ob_get_clean();
}
the_widget just simply calls widget called myWidget.
The point is, everything works good, but id=" " is always empty when I use the second code.
I know it's a Wordpress question, but I believe this has more to do with my PHP code.
Any ideas?
Judging from your last comment the problem is that $args is not set and since the ID is set from $args it will "".
You have to find out where $args is set and either make sure that part is run or set it your self.
$args in this case is not arguments to myWidget method but a global array.
Check if it exists at all
isset($args)
My guess is its a scope issue. $args isnt inside the function. Try passing it to the function