So I have a custom field called 'ingredients'. I want to output this custom field information out onto my custom tab.
This is what I have but it echos out nothing.
function my_custom_tab(){
global $post;
echo get_post_meta($post->ID, 'ingredients', true);
}
Any ideas on why its not echoing anything out?
EDIT: Whatever I put in that last function I need to echo there like this:
add_filter('woocommerce_product_tabs', 'woo_remove_product_tabs', 98);
function woo_remove_product_tabs( $tabs ){
$tabs['my_custom_tab'] = array(
'title' => "My Custom Tab",
'priority' => 15,
'callback' => 'my_custom_tab'
);
return $tabs;
}
function my_custom_tab(){
echo '<h2>This is a title</h2>';
global $post;
return get_post_meta($post->ID, 'ingredients', true);
}
If I insert that echo it will output that in the custom tab. Is there anyway to just say echo 'ingredients', the custom field?
Use return then echo the function.
function my_custom_tab(){
global $post;
return get_post_meta($post->ID, 'ingredients', true);
}
echo my_custom_tab();
return as you can guess, will pass the value back, so when you echo the function it should display the value.
Also make sure that get_post_meta is actually working.
Some background reading:
http://php.net/manual/en/functions.returning-values.php
I figured it out. It was alot simplier than I expected:
function my_custom_tab(){
echo get_post_meta( get_the_ID(), 'ingredients', true );
}
Related
I want to echo PHP variable value on any page using shortcode in WordPress.
I have created:
function custom_shortcode() {
echo $unique;
}
add_shortcode( 'test', 'custom_shortcode' );
When I place shortcode [test] on any page, it returns nothing.
The $unique variable is to show the logged-in user's username in URL. For example: example.com/?username
The expected output on front-end need to be: example.com/?username
try this:
function custom_shortcode( $atts, $content = null ) {
global $unique; // if $unique is global var add this line too
return $unique;
}
add_shortcode( 'test', 'custom_shortcode' );
The following is the shortcode I created in functions.php:
function echo_first_name() {
echo $_GET['first_name'];
}
add_shortcode( 'first_name', 'echo_first_name' );
And I'm entering the following into my Visual Composer editor:
['first_name']
This produces no result, even when using the Visual Composer shortcode mapper.
Does anybody know why this isn't working? Do I have to register it as another type of shortcode for Visual Composer to be able to access it?
For Create Short code
if you want to add short code in editor then Used return instead of echo
function echo_first_name() {
return $_GET['first_name'];
}
add_shortcode( 'first_name', 'echo_first_name' );
Used Short Code Like
[first_name]
If you want to pass the value In shortcode
function echo_first_name( $atts ) {
$a = shortcode_atts( array(
'firstname' => '',
), $atts );
return "First Name= {$a['firstname']}";
}
add_shortcode( 'first_name', 'echo_first_name' );
Used Short Code Like
[first_name firstname="test"]
You are passing a $_GET['firstname'] in the shortcode function from where you are passing this in URL or from any other place. Please check it is coming or not.
Or if you want to test your shortcode is working or not use beloww code it will work.
function echo_first_name() {
return 'testing the shortcode';
}
add_shortcode( 'first_name', 'echo_first_name' );
Use return instead of echo
function echo_first_name(){
return $_GET['first_name'];
}
add_shortcode( 'first_name', 'echo_first_name' );
I have a function in my functions.php file and I need the current post ID.
I have tried getting it like this:
global $wp_query;
$currentID = $wp_query->post->ID;
echo '<pre>';
print_r($currentID);
echo '</pre>';
but doesn't seem to work since it says:
Trying to get property of non-object
EDIT: Entire function in functions.php
add_filter( 'gform_pre_render_1', 'populate_posts' );
add_filter( 'gform_pre_validation_1', 'populate_posts' );
add_filter( 'gform_pre_submission_filter_1', 'populate_posts' );
add_filter( 'gform_admin_pre_render_1', 'populate_posts' );
function populate_posts( $form ) {
global $wp_query;
foreach ( $form['fields'] as &$field ) {
if ( $field->type != 'select' || strpos($field['cssClass'], 'booking-option') === false ) {
continue;
}
$currentID = $wp_query->post->ID;
var_dump($wp_query->post);
$choices[] = array( 'text' => $price, 'value' => $price );
$field->placeholder = '0';
$field->choices = $choices;
}
return $form;
}
Anyone can help me out please
Thanks a lot!
In your function add
global $post;
echo $post->ID;
To have access to the post id you must be within the loop
Otherwise , you must modify your function to accept the post id as a parameter , and hook where is safe to get post id, like so:
add_action('template_redirect', function() {
if (is_single())
your_function(get_queried_object_id());
}
});
function your_function($id){
//Do what you want
}
Some references:
https://wordpress.stackexchange.com/questions/177262/cant-get-post-id-in-functions-php?rq=1
https://wordpress.stackexchange.com/questions/140753/get-current-post-id-in-functions-php
As a note, for the future i think is more appropriate if you post these questions to the http://wordpress.stackexchange.com community
EDIT:
now that you posted the entire code i see that you are using gravity forms (which you didn't mentioned before).
This is a completely different question then.
You must obtain the post_id from the Entry object that gravity forms will pass to your function
https://www.gravityhelp.com/documentation/article/entry-object/
if you want to print the ID then use
the_ID();
if you want to store it then use
$postId = get_the_ID();
Use it in the loop
Hope this helps
Take Care and Happy coding
Getting the current post ID
The ID can be stored as a variable using
<?php $postid = get_the_ID(); ?>
To print
<?php echo $postid; ?>
By function
function get_the_ID() {
$post = get_post();
return ! empty( $post ) ? $post->ID : false;
}
For Reference : click here
I want to check if select field changed it's value after post editing, and if it is, then to send an email to admin.
I saved previous value to a variable $pre_status_eksperimenta using acf/pre_save_post, like this:
function action_pre_post_update( $post_id ) {
$pre_status_eksperimenta = get_post_meta($post_id, 'status', true);
};
add_action( 'acf/pre_save_post', 'action_pre_post_update', 10, 1 );
When I var_dump($pre_status_eksperimenta) I get correct value, wich means it works.
Then I want to pass that to a acf/save_post hook and check if there was a change, but now when I var_dump($pre_status_eksperimenta) I get NULL
function status_change_notification($ID) {
var_dump($pre_status_eksperimenta);
die();
}
add_action( 'acf/save_post', 'status_change_notification', 10, 1);
I think its about variable scope. You should global it when you use it in another function.
function action_pre_post_update( $post_id ) {
global $pre_status_eksperimenta;
$pre_status_eksperimenta = get_post_meta($post_id, 'status', true);
};
and then
function status_change_notification($ID) {
global $pre_status_eksperimenta;
var_dump($pre_status_eksperimenta);
die();
}
So I have had a little search and maybe I am searching for the wrong thing. I am trying to run a function from my functions.php file in wordpress and assign the returned array to a variable.
When I run the function, it just echos out the returned data rather than assigning it to the variable.
I'm assuming this is a Wordpress thing.
Code from functions.php
function get_portfolioBlock( ) {
$portfolio_query = new WP_Query(array('post_type' => 'portfolio', 'posts_per_page' => 1, 'name' => 'test1'));
if ( $portfolio_query->have_posts() ) {
while ($portfolio_query->have_posts() ) {
$portfolio_query->the_post();
$portfolio_block = array('title' => the_title(), 'excerpt' => the_excerpt() );
}
return $portfolio_block;
} else {
return 'error';
}
}
The code in my template file
<?php $portfolio = get_portfolioBlock(); ?>
When the page loads it automatically loads the data into the page without assigning the variable for me to use else where
Thank you Gerald!
The reason for it echoing in out the function was because when I was assigning the_title and the_excerpt I should have been using
get_the_title() and get_the_excerpt()