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.
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.
I am looking for a way to auto tag custom posts in Wordpress without using a plugin.
I have a custom post type 'tech-video' and I want to auto tag it with the video tag every time a post gets published of that type.
I tried this code snippet but it doesn't work:
/* Auto Tag Tech Videos */
add_action('publish_tech_video', 'tag_tech_video', 10, 2);
function tag_tech_video($post_id, $post){
wp_set_post_terms( $post_id, 'tech-video', 'video', true );
}
I'm not skilled with either PHP or Wordpress hooks so any help is appreciated.
Thank you,
You're close; You just got the hook name wrong.
Whenever a post is saved, the following is run:
do_action( "save_post_{$post->post_type}", $post->ID, $post, true );
To leverage this hook you can run:
add_action('save_post_tech_video', 'tag_tech_video', 10, 2);
function tag_tech_video($post_id, $post){
// check the term has not already been added.
$terms = wp_get_post_terms($post->ID, 'video');
$term_names = array_map(function($term){return $term->name;},$terms);
if(!in_array('tech-video',$term_names){
wp_set_post_terms( $post_id, 'tech-video', 'video', true );
}
}
But note: Since the "save_post" hook is run every time the post is saved, you need to check that the term has not already been added.
Note that the signature for wp_set_post_terms is:
function wp_set_post_terms( $post_id = 0, $tags = '', $taxonomy = 'post_tag', $append = false );
So this assumes that you have a registered taxonomy named "video", and the taxonomy is linked to the "tech_video" post type.
After doing some more digging into the Wordpress Codex I was able to figure out a more elegant solution that works great:
// Call when the post gets created
add_action('save_post_tech-video', 'tag_tech_video', 10, 2);
function tag_tech_video($post_id, $post) {
// If the video tag doesn't exist, add it
if (!has_tag('video', $post->ID)) {
wp_set_post_tags( $post_id, 'video', true );
}
}
Note that I had to change tech_video to 'tech-video' to make it match the name defined by the Custom Post Type (and thus call properly).
I like this method because it's cleaner.
Thanks #andrew for pointing me in the right direction at least!
I want to populate the short description field of a WooCommerce product with Gravity Forms.
Unfortunately I couldn't figure out the name of the meta field(?).
Every other meta field works fine. I've checked the code and some docs to find the correct name but I couldn't find it.
I tried the following: excerpt, postexcerpt, post_excerpt.
In the backend code the field's name is excerpt. To use the field's name works for every other meta field.
This snippet will make the short description / excerpt show up as a mappable field:
add_filter( 'gform_advancedpostcreation_excerpt', 'enable_excerpt', 10, 1 );
function enable_excerpt( $enable_excerpt ){
return true;
}
https://docs.gravityforms.com/gform_advancedpostcreation_excerpt/#examples
The great support over at Gravity Forms pushed me in the right direction.
You have to use a custom snippet to map the form field with short description / excerpt:
add_action( 'gform_advancedpostcreation_post_after_creation', 'update_product_information', 10, 4 );
function update_product_information( $post_id, $feed, $entry, $form ){
//update the excerpt
$the_post = array(
'ID' => $post_id,//the ID of the Post
'post_excerpt' => $entry['60'],
);
wp_update_post( $the_post );
}
Here you can find some more informations: https://docs.gravityforms.com/advanced-post-creation-add-on-using-third-party-post-types/#handling-fields-unable-to-be-mapped-2
I am creating a small plugin to add a 'data-pin-description' attribute to my images. I intend for the client to be able to add that meta data description to any image attachment from admin and then have that meta value be output to the tag on the front end, when the attachment is added to post content. I want the plugin to grab that meta data at the appropriate time and include it with the img tag data- attribute and value, before it's already output. End result should be:
My admin code seems to be accepting and saving the client-entered value just find, however I am having trouble outputting that data. One problem is I have been unable to identify which hook would retrieve and add that data to the tag at the correct time
I've tried querying the attachment posts but I don't know what action or filter to hook into
Here is how I'm adding the meta data in admin:
<?php
//exit if file is called directly
if (! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Adding a custom field to Attachment Edit Fields
* #param array $form_fields
* #param WP_POST $post
* #return array
*/
//add attachment fields
function ad_add_pinterest_fields( $form_fields, $post ) {
$field_value = get_post_meta( $post->ID, 'pin-description', true );
$form_fields['pin-description'] = array(
'value' => $field_value ? esc_textarea($field_value) : '',
'label' => __( 'Pin Description' ),
'helps' => __( 'Add a short description for Pinterest SEO' ),
'input' => 'textarea'
);
return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'ad_add_pinterest_fields', null, 2 );
//save attachment fields
function ad_save_pinterest_fields( $attachment_id ) {
if ( isset( $_REQUEST['attachments'][$attachment_id]['pin-description'] ) ) {
$pinDescription = sanitize_text_field( $_REQUEST['attachments'][$attachment_id]['pin-description'] );
update_post_meta( $attachment_id, 'pin-description', $pinDescription );
}
}
add_action( 'edit_attachment', 'ad_save_pinterest_fields' );
?>
I expect to retrieve the post_meta of each attachment image for a given post, but have only received errors
to solve this I ended up hooking into image_send_to_editor to modify the image html prior to it's being sent to the editor. this way, with the image html in the editor and correctly displaying my attribute and value, when I click 'update post' the html is saved to the database with those pieces of data. when the post is viewed on the frontend, the output from the database correctly reflects as well.
I am using WordPress's Advanced Custom Fields 5 plugin to create a front-end form. I would like to save the post title as one of the fields on my form. For example, one of my form fields is 'name' so I would like the post title to be 'John Smith'.
Looking at the ACF documentation it gives example code (copied below) where pre_save_post can be hooked into achieve this. Hoewver, I have included this function and the title still fails to save.
Any ideas what I am doing wrong?
Here is the code:
function my_pre_save_post( $post_id )
{
// check if this is to be a new post
if( $post_id != 'new' )
{
return $post_id
}
// Create a new post
$post = array(
'post_status' => 'draft' ,
'post_title' => $_POST['fields']['field_123'] ,
'post_type' => 'post' ,
);
// insert the post
$post_id = wp_insert_post( $post );
// return the new ID
return $post_id;
}
add_filter('acf/pre_save_post' , 'my_pre_save_post', 10, 1 );
I am using exactly the same method and it works fine.
Just a note that field_123 in your example is not the user defined field name in the wp back end but its actually the field name assigned in the meta_key value in the wp_postmeta table of you db.