Dinamically create a link using ACF (Advanced Custom Field) custom value - php

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.

Related

get submitted form's post_id from outside the loop PHP

I am working on an app that has a post form that matches up adoption mentors with mentees.
This is in Wordpress/PHP/MySQL/ACF (Advanced Custom Field).
After the form has been submitted, I am unable to get the post_id for that post so I can save the ACF field "title" for the screen that lists all the matches.
Do I need to retrieve that form's $post_id when I'm "outside the loop"? And how do I do that?
function match_post_title_auto( $post_id ) {
// get mentor & new mentee user array
$mentee = get_field('match_mentee', $post_id);
$mentor = get_field('match_mentor', $post_id);
$title = ' Mentor: ' . $mentor['display_name'] . ' and Mentee: ' . $mentee['display_name'];
$postdata = array(
'ID' => $post_id,
'post_title' => $title,
'post_type' => 'match'
);
wp_update_post( $postdata );
return $value;
}
var_dump($post_id); //returns NULL
//what do I put here to get that `post_id`? Thanks!
add_action('acf/save_post', 'match_post_title_auto', 10, 1);
The acf/save_post hook only passes the $post_id as single parameter. You have 3 parameters in your callback. https://www.advancedcustomfields.com/resources/acf-save_post/. Also, you need to have a priority higher than 10 to get the updated values from your post.
function match_post_title_auto( $post_id ) {
// do stuff
}
add_filter('acf/save_post', 'match_post_title_auto', 20 );
you are trying to catch the values by filter hook. Actually you need action hook and 1 parameter only. like this:
add_action('acf/save_post', 'match_post_title_auto', 10, 1);
Hope it will work.
for more information, check the standard documentation:
https://www.advancedcustomfields.com/resources/acf-save_post/

How to show post title that I am creating in the content editor?

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).

Get post ID in variable in Grid Builder Visual Composer

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}}';
}

How to get the image attachement custom link with PHP - Wordpress

This will return the link to the attachment:
$link=wp_get_attachment_link($image->ID);
However, I can't find a way to get the LINK TO value from the ATTACHMENT DISPLAY SETTINGS of an image. See screenshot below.
As yoavmatchulsky wrote, this field is dynamically filed by ~wp-includes/js/media-views.js after you manually choose an image
but if you have an id of attachment, use wp_get_attachment_link( $id, $size);
as size use 'full'
full ref. in codex
Or, if you are trying to use a custom link, the method described here might help.
Basically, in your functions.php, you could add a code similar to this:
// Adds a custom url field to your attachment
function attachment_custom_url( $form_fields, $post ) {
$form_fields['video-url'] = array(
'label' => 'CustomURL',
'input' => 'text',
'value' => get_post_meta( $post->ID, 'custom_url', true ),
'helps' => 'Add custom URL, if applicable',
);
return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'attachment_custom_url', 10, 2 );
function attachment_custom_url_save( $post, $attachment ) {
if( isset( $attachment['custom-url'] ) )
update_post_meta( $post['ID'], 'custom_url', esc_url( $attachment['custom-url'] ) );
return $post;
}
add_filter( 'attachment_fields_to_save', 'attachment_custom_url_save', 10, 2 );
And then, you could call it in your php like so:
<?php
Custom Link;
?>
I know this is an old thread, but I solve this by getting the post meta of the attachment, which was easier to me.
In my installation, custom URL input is shown like this:
<input type="text" class="text" id="attachments-140443-foogallery_custom_url" name="attachments[140443][foogallery_custom_url]" value="https://mycustomurl.com">
So, I assumed that if the first brackets contains the post ID, the second one is a meta key to save this value on wp_postmeta table. And there it was, just starting with the underscore character so it would be a hidden meta data. Therefore, the easier way to get this value is like this:
get_post_meta( get_post_thumbnail_id( get_the_ID() ), '_foogallery_custom_url', true);
Of course you need to check if the post does have a thumbnail, but that's easy to adapt.

Pulling data for WordPress Jetpack plugin contact form

I have a small form on my website, which I created using Jetpack plugin (it has built-in contact form creator).
I want to pull options for select input from my custom-post-type "artist" (using the titles of the posts). Is there a way to do it?
[contact-field label='Artist' type='select' required='1' options='i want my titles go here separated by commas plus "other" option'/]
The code for the field looks like this. I believe I need to do some php+jquery stuff in this page template, but I can't seem to get it.
With some creativity, yes, it's possible :)
We create another Shortcode to create a "virtual" JetPack shortcode.
I tested this using the default post post_type.
add_shortcode( 'my-jet-form', 'so_14003883_jet_form' );
function so_14003883_jet_form( $atts, $content )
{
// Query our post type, change accordingly
$posts = get_posts( array(
'post_type' => 'post',
'numberposts' => -1,
'post_status' => 'publish'
) );
// Build an array of post titles
$titles = array();
foreach( $posts as $post )
{
$titles[] = $post->post_title;
}
// Convert array into comma sepparated string
$posts_select = implode( ',', $titles );
// Make JetPack shortcode
$return = do_shortcode( '[contact-form][contact-field label="Name" type="name" required="1"/][contact-field label="Artist" type="select" options="' . $posts_select . '"/][/contact-form]' );
return $return;
}
Usage:
adjust the desired post type
adjust the do_shortcode part to suit your original shortcode
put the shortcode [my-jet-form] wherever you want
voilà

Categories