Using A Global Variable with its Value on another Page (PHP) - php

I have a wordpress site where Iv assigned a global Variable $rushad and given it the value of a text box found on my product page.
global $rushad;
if((isset($_POST['tmcp_textfield_0'])) && !empty($_POST['tmcp_textfield_1']))
{
$rushad= $_POST['tmcp_textfield_0']; //note i used $_POST since you have a post form **method='post'**
}
I now want to use this information to trigger an email from a plugin Im using.
To do this I modified the plugins code like this....
function woocommerce_gift_coupon_send_action( $post_ids ) {
global $wpdb;
require_once WOOCOMMERCE_GIFT_COUPON_DIR . 'includes/mail-template.php';
$generated_coupon = 0;
foreach ( $post_ids as $post_id ) {
$order = new WC_Order( $post_id );
$items = $order->get_items();
$mailto = $rushad;
$coupons_mail = array();
$sc = false;
$coupons_to_generated = woocommerce_gift_coupon_check_order_coupons_count( $post_id );
$coupons_generated = woocommerce_gift_coupon_check_order_coupons( $post_id );
But its Not working yet.
Any Advice on where Im messing up?

Variable $rushad will not work because it is not defined.
This will work like this
global $rushad;

Related

How to update order item meta data from everywhere?

I am developing a cycle renting site in which user select the date range (in days) from start date to end. And I am storing that data using wc_add_order_item_meta() function as a meta data of the order, right.
Then, I wanted to give user a functionality like he could change or extend the days by going through the orders page. This is also done, I have created a modal and added a ajax request on change of dates on modal.
Now, I sent data to functions.php using AJAX and I used wc_update_order_item_meta() to update the meta data.
Here's my function looks like :
add_action( 'wp_ajax_update_date_picker', 'update_date_picker' );
function update_date_picker() {
$p_da = $_POST['p_date'];
$r_da = $_POST['r_date'];
$t_dy = $_POST['t_days'];
$order_id = $_POST['order_id'];
$order = wc_get_order($order_id);
foreach ($order->get_items() as $item_id => $item_obj) {
$pd = wc_update_order_item_meta($item_id, 'pickup_hidden_datetime', $p_da);
$rd = wc_update_order_item_meta($item_id, 'return_hidden_datetime', $r_da);
$td = wc_update_order_item_meta($item_id, 'return_hidden_days', $t_dy);
if($pd&&$rd&&$td){
echo "Nice work, Bilal";
}
die();
}
}
This is updating too but not at all the places. I mean, it's giving me right response, when I refresh, it's also giving the updated data but when I go through the view item page it's showing there the old data only instead of updated one. Even when I check the orders in the admin, it's showing the old meta data there too.
Any idea would be appreciated, Thanks in Advance.
I think it's because of the cache that WooCommerce has... try something like this.
add_action( 'wp_ajax_update_date_picker', 'update_date_picker' );
function update_date_picker() {
$p_da = $_POST['p_date'];
$r_da = $_POST['r_date'];
$t_dy = $_POST['t_days'];
$order_id = $_POST['order_id'];
$order = wc_get_order($order_id);
foreach ($order->get_items() as $item_id => $item_obj) {
$pd = wc_update_order_item_meta($item_id, 'pickup_hidden_datetime', $p_da);
$rd = wc_update_order_item_meta($item_id, 'return_hidden_datetime', $r_da);
$td = wc_update_order_item_meta($item_id, 'return_hidden_days', $t_dy);
if($pd&&$rd&&$td){
echo "Nice work, Bilal";
}
}
clean_post_cache( $order->get_id() );
wc_delete_shop_order_transients( $order );
wp_cache_delete( 'order-items-' . $order->get_id(), 'orders' );
die();
}

WordPress; File renaming on upload

I'm trying to rename files while uploading them to WordPress and I want them to get the name of sanitized post title.
Basically I want to do same thing as here, but unfortunately when I use the code from this answer - I don't get the value of $post variable.
The only thing I get is "empty" name with some numbers at the end and the file extension, e.g. "-5263.png", which increses with every new file.
For some reason I don't get the $post value which would give me the post title and it just changes the file name to... well, nothing and just adding some numbers at the end, so it doesn't overrite any other file.
I really would like to know what's wrong with my code:
function new_filename( $filename, $filename_raw ) {
global $post;
$info = pathinfo( $filename );
$ext = empty( $info['extension'] ) ? '' : '.' . $info['extension'];
$new = $post->post_title;
if ( $new != $filename_raw ) {
$new = sanitize_file_name( $new );
}
return $new . $ext;
}
add_filter( 'sanitize_file_name', 'new_filename', 10 );
Thank you in advance for your help.
I've made a plugin a long time ago called File Renaming on Upload that can help you on this, but if you are searching for help with your code i can say that you can try a different approach to get the post variable. Try this instead:
function get_post() {
global $post;
$post_obj = null;
if( !$post ){
$post_id = isset( $_REQUEST['post_id'] ) ? $_REQUEST['post_id'] : false;
if ( $post_id && is_numeric( $post_id ) ) {
$post_obj = get_post( $post_id );
}
}else{
$post_obj = $post;
}
return $post_obj;
}
Once you get your post variable, you don't need to use the post_title like that. You can use
$post->post_name
And then you don't need to use sanitize_file_name() function

How may I get the id of a div, or any other div information during the save_post hook?

I have a div with inputs within it in my document, and I would like to be able to save more than just each input's value in a function, run during the save_post hook. for testing, it looks like this at the moment:
$sanitized_data = array();
foreach ( $resources as $index=>$resource ) {
$resource = esc_url( strip_tags( $resource ) );
if ( ! empty( $resource ) ) {
$sanitized_data[$index] = $resource;
}
}
$myDocument->update_post_meta(
$post_id,
'Meta_key',
$sanitized_data
);
I'm not sure how to retrieve any other information, and I suspect when the save_post action is run the document no longer exists, only the meta data, or at least that's the way it seems.
I created this function to try and see what was going on:
$elements = $this->getElementsByTagName('input_wrapper');
foreach($elements as $index=>$child) {
var_dump( $child );
}
die();
Unfortunately, this comes up with nothing but a blank screen. Can anyone help me?

Getting Next/Previous Post ID using Current Post ID in Wordpress

I want to write a custom next/prev function to dynamically display post information in a fancybox pop-up. So I need to use PHP to get the next and previous Post ID based on whatever Post is currently showing. I know what the current post ID is and I can send that to a function ok, but I can't figure out how to use that ID to obtain the adjacent IDs.
Edit:Here is my code so far (which is not working)
<?php
require_once("../../../wp-blog-header.php");
if (isset($_POST['data'])){
$post_id = $_POST['data'];
}else{
$post_id = "";
}
$wp_query->is_single = true;
$this_post = get_post($post_id);
$in_same_cat = false;
$excluded_categories = '';
$previous = false;
$next_post = get_adjacent_post($in_same_cat,$excluded_categories,$previous);
$post_id = $next_post->id;
$title = $next_post->post_title;
$dataset = array ( "postid"=>$post_id, "posttitle"=>$title );
//Because we want to use json, we have to place things in an array and encode it for json.
//This will give us a nice javascript object on the front side.
echo json_encode($dataset);
?>
get_adjacent_post() uses the global $post as its reference point, so you'll want to replace this:
$this_post = get_post($post_id);
with this:
global $post;
$post = get_post($post_id);
WordPress also provides get_next_post() and get_previous_post(), which you can use here instead of using get_adjacent_post() with all of those arguments. Here's the final product:
<?php
require_once("../../../wp-blog-header.php");
if (isset($_POST['data'])){
$post_id = $_POST['data'];
}else{
$post_id = "";
}
$wp_query->is_single = true;
global $post;
$post = get_post($post_id);
$previous_post = get_previous_post();
$next_post = get_next_post();
$post_id = $next_post->id;
$title = $next_post->post_title;
$dataset = array ( "postid"=>$post_id, "posttitle"=>$title );
//Because we want to use json, we have to place things in an array and encode it for json.
//This will give us a nice javascript object on the front side.
echo json_encode($dataset);
?>
I'm not sure what keys you'd like to use for the IDs and titles of the previous and next posts in the $dataset array, so I'll leave that as is for now.
To get this to work I had to change $next_post->id; to $next_post->ID;, i.e. capitalise ID.
I used it in Wordpress on index.php like this – I inserted this at the top of the loop:
<div class="work-post" id="<?php the_ID(); ?>">
then within the loop I use this:
`
if (isset($_POST['data'])){
$post_id = $_POST['data'];
}else{
$post_id = "";
}
$wp_query->is_single = true;
global $post;
$post = get_post($post_id);
$next_post = get_next_post();
$previous_post = get_previous_post();
?>
<!-- call only if a value exists -->
<?php if($next_post) : ?>
<div>PREV.</div>
<?php endif; ?>
<!-- call only if a value exists -->
<!-- call only if a value exists -->
<?php if($previous_post) : ?>
<div>NEXT</div>
<?php endif; ?>
<!-- call only if a value exists -->`
This is my code, it worked good. $post_id is current post ID.
function get_previous_post_id( $post_id ) {
// Get a global post reference since get_adjacent_post() references it
global $post;
// Store the existing post object for later so we don't lose it
$oldGlobal = $post;
// Get the post object for the specified post and place it in the global variable
$post = get_post( $post_id );
// Get the post object for the previous post
$previous_post = get_previous_post();
// Reset our global object
$post = $oldGlobal;
if ( '' == $previous_post )
return 0;
return $previous_post->ID;
}
function get_next_post_id( $post_id ) {
// Get a global post reference since get_adjacent_post() references it
global $post;
// Store the existing post object for later so we don't lose it
$oldGlobal = $post;
// Get the post object for the specified post and place it in the global variable
$post = get_post( $post_id );
// Get the post object for the next post
$next_post = get_next_post();
// Reset our global object
$post = $oldGlobal;
if ( '' == $next_post )
return 0;
return $next_post->ID;
}
Then you just call function. Example:
echo get_previous_post_id( $current_id );
echo get_next_post_id( $current_id );
Good luck!

Scope: Using Variable Defined in a Function

I have a function in a functions.php file that defines certain variables:
add_action( 'the_post', 'paginate_slide' );
function paginate_slide( $post ) {
global $pages, $multipage, $numpages;
if( is_single() && get_post_type() == 'post' ) {
$multipage = 1;
$id = get_the_ID();
$custom = array();
$pages = array();
$i = 1;
foreach( get_post_custom_keys() as $key )
if ( false !== strpos( $key, 'slide' ) )
$custom[$key] = get_post_meta( $id, $key, true);
while( isset( $custom["slide{$i}-title"] ) ) {
$page = '';
$tzTitle = $custom["slide{$i}-title"];
$tzImage = $custom["slide{$i}-image"];
$tzDesc = $custom["slide{$i}-desc"];
$tzEmbed = $custom["slide{$i}-embed"];
$page = "<h2>{$tzTitle}</h2><img src='{$tzImage}' />";
$pages[] = $page;
$i++;
}
$numpages = count( $pages );
}
}
I'd like to output some of these variables in a template.php file like so: <?php echo $tzDesc; ?> but I can't seem to get it to work. From what I understand about the variables scope, in order to call these variables in another place I need to define them within the global scope and call them as global in this function like I did the $pages, $multipage, $numpages;. That should allow me to plug those variables in where I need them. The problem is when I take them out of the function and define them above within the global scope the entire function stops working.
How do I need to structure this so I can call <?php echo $tzDesc; ?> anywhere in the site and have it echo the defined info?
I don't know if this matters but this is on a WordPress site.
If you want to use <?php echo $tzDesc; ?> anyway, you would need to define $tzDesc as a global variable. However, I don't recommend doing so as global variables are considered poor programming practice.
A better solution would be to have the paginate_slide() add $tzDesc (and other values) to the $post object. That way you have access to these variables anytime you call the_post(). If you go this route, be sure to namespace you variables:
$post->ns_tzDesc = $tzDesc;

Categories