I’m trying to get some custom fields inside my custom grid builder. I have added some extra taxonomy and would like to add custom data to display. I’ve read your article here: https://kb.wpbakery.com/docs/developers-how-tos/adding-custom-shortcode-to-grid-builder/ and when implementing it, I’m getting a problem when trying to get the id of the current post ID. I know the code is as follows:
add_shortcode( 'vc_post_id', 'vc_post_id_render' );
function vc_post_id_render() {
return '<h2>{{ post_data:ID }}</h2>'; // usage of template variable post_data with argument "ID"
}
The thing is that the {{ post_data:ID }} cannot be saved to a variable to later get the post and play with it as such:
$post_id = '{{ post_data:ID }}';
$post = get_post($post_id);
as it will fail. Could you please tell me how to get the current post ID as a variable so I can show custom data on the grid?
Thank you very much.
Ok, here what I'm thinking. In my scenario, I have a custom field called price.
So I was able to show the price by using
{{ post_data:price }}
This. But when I was trying to assign it to a variable, it failed. When I var_dump the variable it gives me (21) characters for every time. So I thought there must be invisible characters. so I
echo bin2hex($price)
The result was 7b7b20706f73745f646174613a7072696365207d7d
And after ASCII to text conversion, it became this
{{ post_data:price }}
Then I realized it. Oh silly me. These are template tags. Like in smarty or angular. They injected values once the page has loaded. So PHP doesn't have a chance get value because everything happening on the client side.
you need to create vc_gitem_template_attribute_YOUR_ATTRIBUTE and there you can take id. like this:
add_filter( 'vc_gitem_template_attribute_producer_logo', 'vc_gitem_template_attribute_producer_logo', 10, 2 );
function vc_gitem_template_attribute_producer_logo( $value, $data ) {
extract( array_merge( array(
'post' => null,
'data' => '',
), $data ) );
$termini = get_the_terms( $post->ID, 'producer' );
$logo = get_field('prlogo', $termini[0]);
$image = '<img class="img-prod" src="' . $logo . '">';
return $image;
}
and render
add_shortcode( 'producer_logo', 'vc_producer_logo_render' );
function vc_producer_logo_render($atts, $content, $tag) {
return '{{producer_logo}}';
}
Related
I'm trying to create a custom link based on a custom field, something like this:
<a href='htts://wa.me/55[acf field="phone-number"]?text=more%20text%here'>Whatsapp</a>
Maybe creating another shortcode loading de ACF field, but I don't know how do that.
I've tried do customized the following code, but without success:
function diwp_enclosed_shortcode_social_links($attr, $content){
$args = shortcode_atts( array(
'url' => '#',
'color' => '#F0F',
'textsize' => '16px'
), $attr );
$output = ''.$content.'';
return $output;
}
add_shortcode( 'enclosed_social_links', 'diwp_enclosed_shortcode_social_links' );
Hello as explained in the documentation you can load the acf field just by adding the id of the post it is associated with:
$value = get_field( "phone-number", 123 );
You can find the post id in the url on the edit post in the backend for example: https://your-url/wp-admin/post.php?post=161&action=edit
In that case we will get the phone-number from the post 161 and it should all be set, if the whole thing need to be done dynamically then we can just use get_field() because we should be in the page in which the field is saved.
Merry christmas!
I solved my problem with the following code:
function numero_whatsapp_dinamico( $attr ) {
$post_id = $attr['post_id'];
$phone_number = get_field( 'numero_de_whatsapp', $post_id );
$output = '<a class="botao-whatsapp-estabelecimento" href="https://wa.me/55' . $phone_number . '?text=more%20text%20here" ">Whatsapp</a>';
return $output;
}
add_shortcode( 'numero_whatsapp_estabelecimento', 'numero_whatsapp_dinamico' );
Hope It'll help someone else with the same problem.
Is this possible in Wordpress? I am trying to use a pre-defined template for my contents. To do that, I use something like this:
add_filter( 'default_content', 'custom_editor_content' );
function custom_editor_content( $content ) {
$args = array(
'posts_per_page'=> 15,
'orderby' => array(
'ID' => 'DESC' ,
),
);
$query = new WP_Query( $args );
$query_contents=Array();
while ( $query->have_posts() ) {
$query->the_post();
array_push($query_contents,Array(
"id"=>get_the_ID(),
"title"=>get_the_title(),
"url"=>get_permalink(),
));
}
$content = '
'.get_the_title( $id ).'
';
return $content;
}
But I can't get the post title (the one I am creating at that moment), somehow. Does someone know how to do this? If I put the while statement into $content, it shows the whole query in the editor and that's not what I want of course.
All I want to do is to fetch the post title and show it in the content editor (after posting or before, that wouldn't matter)
Could someone help me out?
I think that at the time of execution of this script (which is before the page loads) the post you are "creating" doesn't exists yet and therefore you cannot fetch its title.
You can try following, as the default_content supports second argument, which is the edited post:
add_filter( 'default_content', 'custom_editor_content' );
function custom_editor_content( $content, $post ) {
...
$content = '
'.$post->post_title.'
';
return $content;
}
However I think you will be facing the same issue as mentioned at the beginning of my answer. If you want to add title to content for new post, you will probably need a javascript which will copy the title into content as you write it (only if the content is still empty).
I am trying to access ACF data from another page to be displayed on another using Timber (Twig).
The ACF name is the_unstrung_hero in the "About" page (id = 7).
page-home.php:
<?php
$context = Timber::get_context();
$post = new TimberPost();
$about_page_id = 7;
$about = new TimberPost($about_page_id);
$about->acf = get_field_objects($about->ID);
$context['post'] = $post;
Timber::render( array( 'page-' . $post->post_name . '.twig', 'page.twig' ), $context );
Within page-home.twig:
<p>{{ acf.the_unstrung_hero|print_r }}</p>
This is just the last combination attempt of many. Frankly I am just not getting something (PHP is not a forte of mine)... Your help will be greatly appreciated.
In your example above, I see that you get the field data from the about page, but you’re not adding it to the context. Your template doesn’t display that data, because you didn’t hand it over to the template.
You set up your context first:
$context = Timber::get_context();
Then you get the current post data that should be displayed:
$post = new TimberPost();
Now you did load $post, but it’s not in your context yet. You have to put the data you want to display on your page into the $context array. Then you render it trough Timber::render( 'template.twig', $context ). Your Twig template will only contain data that is is present in $context (to be complete: you can also use functions in your Twig templates to get data, but that is another topic).
To also add the data you loaded from the about page, you’d have to do this:
$about_page_id = 7;
$about = new TimberPost( $about_page_id );
$context['about'] = $about;
See that the line $about->acf = get_field_objects($about->ID) is not there anymore? You don’t need it, because Timber automatically loads your ACF fields into the post data. Your field would now be accessible through {{ about.the_unstrung_hero }} in your Twig template.
To come back to what you want to achieve:
I’d solve it like this.
Like Deepak jha mentionend in the comments of your question, I’d also make use the second parameter of the get_field() function to get field data from a post by post ID.
You don’t really need to load the whole post of the about page if you just want to display the value of one ACF field.
page-home.php
$context = Timber::get_context();
$post = new TimberPost();
$context['post'] = $post;
// Add ACF field data to context
$about_page_id = 7;
$context['the_unstrung_hero'] = get_field( 'the_unstrung_hero', $about_page_id );
Timber::render( array( 'page-' . $post->post_name . '.twig', 'page.twig' ), $context );
And then in within page-home.twig you can access the field data in post.
<p>{{ the_unstrung_hero }}</p>
Working on a Wordpress site and where I need to have a post with php content.
I figured out that this is only possible with a plugin or a shortcode in the functions.php
Googled around, tried a lot but it isn't working out, so i definitely doing something wrong.
code I have in functions.php:
function anniversary_link($text) {
$url = the_permalink();
return "<a href='$url'>$text</a>";
}
add_shortcode('permalink', 'anniversary_link');
And the post I have:
and the result I get when clicking the link:
The shortcode has to reference to the single.php page and al the static code's references to the single.php page is just:
<?php the_permalink() ;?>
Is this te 'correct' way to use href on a post (is there a better/cleaner way to get this working)?
Edit
Updated my code thanks to nathan Edit edit: in functions.php
function anniversary_link( $atts ) {
$atts = shortcode_atts( array(
'text' => '',
), $atts, 'permalink' );
$url = get_permalink();
return '' . $atts['text'] . '';
}
add_shortcode('permalink', 'anniversary_link');
And how I use this short code inside a post (I think that I incorrectly use the shortcode):
Result:
Edit Edit
This is how I call the dynamic anniversary post:
<?php echo get_posts(array( 'category_name' => 'Anniversary' ))[0]->post_content ;?>
(inside the header)
solution thanks to nathan
Reading through the code you've posted I see three issues.
The way you're accessing the 'text' attribute.
The function you're using to get the permalink.
The way you're inserting your shortcode into your content.
Shortcode Attributes
The first parameter of a shortcode callback should be an array of attributes, not a single string. Naming your parameter $text has no bearing on the value and won't pull the text attribute of your shortcode.
Change the name of your parameter from $text to $atts and set a default value for the text attribute. Setting a default value is a good practice with shortcodes and can be done using the shortcode_atts() function.
Return vs. Output Functions
The second issue is your use of the_permalink(). the_permalink() doesn't return a permalink but outputs it directly instead. As such you can't assign it to a variable.
The new function
function anniversary_link( $atts ) {
// Set defaults where needed
$atts = shortcode_atts( array(
'text' => '',
), $atts, 'permalink' );
// Replace the_permalink().
// Given the level of simplicity it doesn't need it's own variable.
$url = get_permalink();
// Put together a new return statement.
// Various ways this could be formatted. I went with something clear and easy to understand.
return '' . $atts['text'] . '';
}
Usage
In your code you're using the shortcode inside the href attribute of a link. The shortcode returns a full link, not a URL, and therefore shouldn't be inside another a tag.
Example:
[permalink text="My Link Text"]
// Outputs My Link Text
Hi all, thanks for reading.
Environment :
Wordpress + Advanced Custom Fields plugin
Problem :
I have searched for hours now and I can't seem to find the correct syntax to do the following:
When posting a new post, get custom field value in order to automatically replace the title of the post by that value. Example: I create a post and set '10am' in my 'time' custom field. The title of the post is automatically replaced by '10am'.
Example:
So I'm adding a filter with the following :
add_filter('wp_insert_post_data', 'change_title') ;
function change_title($data)
{
$time = XXX ; // How should I get this custom field value ?
$new_title = 'Topic created at'.$time ;
$data['post_title'] = $time ;
return $data;
}
It must be very simple but I have tried every function available on both WP and the plugin's documentations. I would be very thankful if anyone passing by gave me the solution.
Thanks in advance !
Tweak to Riadh's accepted answer (would add as a comment but haven't got enough rep yet):
As documented in the WordPress Codex wp_update_post includes the save_post hook so calling wp_update_post() inside the save_post hook creates an infinite loop. To avoid this, unhook then rehook your function like so:
add_action('save_post', 'change_title');
function change_title($post_id) {
$time = get_field('time',$post_id);
$post_title = 'Topic created at '. $time;
// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'change_title');
// update the post, which calls save_post again
wp_update_post(array('ID' => $post_id, 'post_title' => $post_title));
// re-hook this function
add_action('save_post', 'change_title');
}
You can actually access the global $_POST variable for your field value , but i guess you can do it in a cleaner way by using the save_post action to update your post's title, eg:
add_action('save_post', 'change_title');
function change_title($post_id) {
$time = get_field('time',$post_id);
$post_title = 'Topic created at '. $time;
// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'change_title');
// update the post, which calls save_post again
wp_update_post(array('ID' => $post_id, 'post_title' => $post_title));
// re-hook this function
add_action('save_post', 'change_title');
}
assuming that your ACF fieldname is "time".
Edit: Updated the answer as per Mark Chitty's answer.
You may try this
add_filter( 'wp_insert_post_data', 'change_title', '99', 2 );
function change_title($data , $postarr){
$custom_field = 'custom_filed_name';
$post_id = $postarr['ID'];
$time = get_post_meta( $post_id, $custom_field, true );
// Now you have the value, do whatever you want
}
Advanced Custom Fields creates a 'field key' for each custom field that is created. I was able to refer to this key value when trying to access the custom fields. The field key value can be found by viewing page source when viewing the post type within the Wordpress admin section.
Look for data-field-key. You will see a value similar to data-field-key="field_5847b00820f13" in the page source. Use this value when accessing the value in the $postarr argument in the wp_insert_post_data filter. The custom fields will be in a nested array named fields within the $postarr argument.
Alternatively, the field key value can be located by navigating to the Advanced Custom Fields / Export option from within the admin section. Once you are on the export page for Advanced Custom Fields, select the export to PHP option and you will see the value in the resulting PHP code.
In the example below, I am concatenating two Advanced Custom Fields and updating the post_title in the $data array returned from the function.
The result is that the post_title value will be saved to the database via the built in Wordpress save post logic.
add_filter('wp_insert_post_data', 'slb_set_title', '99', 2);
function slb_set_title ($data, $postarr){
if($data['post_type']==='slb_subscriber'){
$data['post_title'] = $postarr['fields']['field_5847b00820f13'] .' '.
$postarr['fields']['field_5847b03f20f14'];
}
return $data;
}