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;
Related
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;
I created this code to display the location from ACF that matches part of the URL and it works as expected.
<?php
$myurl= htmlspecialchars($_SERVER["REQUEST_URI"]);
$myexplodes = ( explode ('/', $myurl) );
$posts = get_posts(array(
'post_type' => 'my_vars',
));
if( $posts ){
foreach( $posts as $post ){
$value = get_field( "location" );
//echo get_field( "location" );
if( $value == $myexplodes[1]) {
echo '<h1>' . $value . ' :this is location</h1>';
}
else {
}
}
}
?>
But when I try to place this code into a function nothing is displayed when I call it.
function local (){
if( $posts ){
foreach( $posts as $post ){
$value = get_field( "location" );
//echo get_field( "location" );
if( $value == $myexplodes[1]) {
echo '<h1>' . $value . ' :this is location</h1>';
}
else {
}
}
}
}
I suspected that it is a scope problem with the vars but I have tried to make the vars global but had no luck.
The first one probably works because $post is a WordPress global variable, so I advise to use another variable name in your foreach.
For the thing that you want to do you should use also the post id in the get_field function call:
$value = get_field( "location", $article->ID );
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
i want add a new field "title" on comment of wordpress, after insert the new input field in a default form of wordpress i added this in my function.php for save the title when new comment are submitted.
this is the code i use for save title:
function add_comment_meta_values($idcommento) {
global $post;
$idcommento= get_comment_ID();
$tipodipost= get_post_type($post->ID);
if( get_post_type($post->ID) == 'service') {
if(isset($_POST['title_svz']) ) {
$title= wp_filter_nohtml_kses($_POST['title_svz']);
add_comment_meta( $idcommento , 'title_svz', $title, false);
}}
}
add_action ('comment_post', 'add_comment_meta_values', 1);
this code work only when remove the condition :
if( get_post_type($post->ID) == 'service') {}
and i don't understand why, i have already tried this condition in comment.php or in a footer with simple function like this
function test_function() {
if( get_post_type($post->ID) == 'service') { echo 'done'; }
}
add_action( 'wp_footer', 'test_function' );
and it's work, so i don't understand why don't work in my primary code, any idea ?
SOLVED MYSELF
THIS IS THE NEW CODE:
function add_comment_meta_values($idcomment) {
$comment_full = get_comment( $idcomment );
$idpost = $comment_full->comment_post_ID;
$typepost= get_post_type($idpost);
if( $typepost == 'service') {
if(isset($_POST['title_svz']) ) {
$title= wp_filter_nohtml_kses($_POST['title_svz']);
add_comment_meta( $idcomment , 'title_svz', $title, false);
} }
}
add_action ('comment_post', 'add_comment_meta_values', 10, 1);
Sometimes in Wordpress, depending on the Context, the global $post may give you unexpected result. Thus something like $post->ID may not point to the appropriate ID you are looking for. You may as well try inspecting the $post object to see if it is what you had expected; like so:
<?php
function add_comment_meta_values($idcommento) {
global $post;
// TRY DUMPING THE $post VARIABLE TO SEE WHAT YOU GET
var_dump($post->ID);
var_dump($post->post_type);
var_dump($post);
exit;
$idcommento = get_comment_ID();
$tipodipost = get_post_type($post->ID);
// ALTHOUGH THIS IS ESSENTIALLY THE SAME AS WHAT YOU DID
if( $post->post_type == 'service') {
if(isset($_POST['title_svz']) ) {
$title= wp_filter_nohtml_kses($_POST['title_svz']);
add_comment_meta( $idcommento , 'title_svz', $title, false);
}}
}
add_action ('comment_post', 'add_comment_meta_values', 1);
After the Inspection, you'd sure know where and what was not OK in your Code...
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!