ACF Remove Image From WP Media Library (with php code) - php

I am using Advanced Custom Fields Pro and ACF Frontend.
I am trying to remove the image from wordpress media library when a user deletes the image from the image field.
This is what I did:
//ACF Remove Image From WP Media Library
function delete_image( $value, $post_id, $field ) {
$old_value = get_field( $field['local-avatar'], $post_id, false /* Don't format the value, we want the raw ID */ );
if ( $old_value && ( int ) $old_value !== ( int ) $value )
wp_delete_attachment( $old_value, true );
return $value;
}
add_filter( 'acf/update_value/type=image', 'delete_image', 10, 3 );
I got to this by googling:
https://wordpress.stackexchange.com/questions/199887/wordpress-acf-delete-image-from-media-library
Furthermore, to find a solution, I also searched and documented myself on: https://www.advancedcustomfields.com/resources/
Unfortunately it's not working for me, I don't understand what I'm doing.
I am new to Wordpress and php, does anyone have any idea how to solve this problem ?

Related

WP Grid Builder populate grid from custom field (ACF gallery)

I would like to populate WPGRIDBUILDER grid from ACF Gallery field.
Support told me:
Grids only work with WOrdPress content type: (custom) post types, (custom) taxonomy terms and WordPress users.
So, what you are looking for requires to use custom PHP code in order to populate a grid from ACF field.
And the field must hold image IDs uploaded in WordPress media library.
Here are two snippets which I assume can be the answer. I'm totally newbie in PHP.
https://docs.wpgridbuilder.com/resources/filter-grid-query-args/
function prefix_query_args( $query_args, $grid_id ) {
if ( 1 === $grid_id ) {
$query_args['post__not_in'] = [ 1, 2, 3, 4 ];
}
return $query_args;
}
add_filter( 'wp_grid_builder/grid/query_args', 'prefix_query_args', 10, 2 );
https://support.advancedcustomfields.com/forums/topic/gallery-field-return-images-ids/
$images = get_field('gallery_field'); //gallery field
foreach( $images as $img ):
$img_srcset = wp_get_attachment_image_srcset($img['ID']); // return Image ID for each in gallery
Here is my take on it:
function prefix_query_args( $query_args, $grid_id ) {
if ( 2 === $grid_id ) { //mygrid id
$query_args['post_type'] = [ 'realizacje-cpt' ];//my cpt
$images = get_field('gallery-cf'); //gallery field
foreach( $images as $img )
$img_srcset = wp_get_attachment_image_srcset($img['ID']);
}
return $query_args,$images;
}
add_filter( 'wp_grid_builder/grid/query_args', 'prefix_query_args', 10, 2 );
Em I even going in right direction?

Visual Composer adds ?id= to background images of columns and rows

I'm trying to speed up a WordPress website which uses Visual Composer plugin. In GTmetrix result I see this:
Serve resources from a consistent URL
https://example.com/wp-content/uploads/2015/02/sfondo_form.jpg?id=15129
The image with query string ?id=XXX is background of a column in Visual Composer. How can I disable it?
All these queries are in the VC custom shortcode. Check the picture below:
In case that might be helpful to anyone - I modified regex query to fetch all background styles:
$value = preg_replace('/(url\(\S+\.(jpg|jpeg|png|svg|gif))\?id=\d+(\))/', '$1$3', $value);
Background styles save in post_meta table. Here is how VC add custom CSS in a page body:
$shortcodes_custom_css = get_post_meta( $id, '_wpb_shortcodes_custom_css', true );
if ( ! empty( $shortcodes_custom_css ) ) {
$shortcodes_custom_css = strip_tags( $shortcodes_custom_css );
echo '<style type="text/css" data-type="vc_shortcodes-custom-css">';
echo $shortcodes_custom_css;
echo '</style>';
}
So we can filter post_meta value and VC output as we want:
add_filter('get_post_metadata', 'theme_prefix_vc_fix_shortcodes_custom_css', 10, 4);
function theme_prefix_vc_fix_shortcodes_custom_css($value, $object_id, $meta_key, $single) {
if ($meta_key!='_wpb_shortcodes_custom_css' || is_admin()) {
return $value;
}
// filter data for _wpb_shortcodes_custom_css only and not for admin
remove_filter('get_post_metadata', 'theme_prefix_vc_fix_shortcodes_custom_css', 10);
$value = get_metadata('post', $object_id, $meta_key, $single);
// we need to remove filter here because get_metadata function use "get_post_metadata" hook so we will have loop if not remove filter
if ($value) {
// you can use another regular expression, this is what I created quickly
$value = preg_replace("/(background-image[:\s]+url\(\S+\.(jpg|jpeg|png|svg|gif))\?id=\d+(\))/", "$1$3", $value);
}
add_filter('get_post_metadata', 'theme_prefix_vc_fix_shortcodes_custom_css', 10, 4);
return $value;
}
Alternatively, you can use the_content filter to remove ?id=XXXX from background images in document body.

Add fields in Json Response of Posts REST API

I want to add a custom field in a response of posts, but when I add new fields after a while it is deleted from the file what I do.
The file that I modify its class-wp-rest-posts-controller.php and I add the new field in prepare_item_for_response function
I add this lines, that works well, but it's deleted after a time
foreach ( $taxonomies as $taxonomy ) {
$base = "other";
$terms = get_the_terms( $post, $taxonomy->name );
$datres = $terms ? array_values( wp_list_pluck( $terms, 'name' ) ) : array();
$data[ $base ] = implode(" ", $datres);
}
Any idea why this happens?
In WordPress you shold not edit core files or core plugins, your changes are deleted becuase the files are updated. The right way do to it is thrugh Hooks and functions from a child theme / your own plugin.
WP Child theme
WP Rest api custom end point docs

woo commerece short codes not working on all posts

I have created a short code to display short description in woo commerce but it is not working on all posts. It is displaying the short description on some posts and not on others.
Function to create that short code in functions.php
function product_shortdesc_shortcode( $atts ){
// use shortcode_atts() to set defaults then extract() to variables
extract( shortcode_atts( array( 'id' => false ), $atts ) );
// if an $id was passed, and we could get a Post for it, and it's a product....
if ( ! empty( $id ) && null != ( $product = get_post( $id ) ) && $product->post_type = 'product' ){
// apply woocommerce filter to the excerpt
echo apply_filters( 'woocommerce_short_description', $product->post_excerpt );
}
}
// process [product_shortdesc] using product_shortdesc_shortcode()
add_shortcode( 'product_shortdesc', 'product_shortdesc_shortcode' );
The way i am getting the data in my single.php file
$custom = get_post_custom(get_the_ID());
$my_custom_field = $custom['woo_id'];
foreach ( $my_custom_field as $key => $value ) {
echo do_shortcode('[product_shortdesc id='.$value.']');
}
PS: in my normal post i have a custom field which has the value of product id of the product in woo commerece.
Your issue is that you are expecting shortcodes to function which no longer exist. On new installs, these pages won't be created, but if you are updating you may already have those pages in place.
Although the upgrade script does attempt to trash them for you, this might not have happened if they were customised. Delete them. Delete edit-account and change password, then go to your 'my account' page and click the change password/edit account links. You'll be taken to and endpoint which offers the same functionality.
Thanks
Short Code must not echo code instead return the things that needs to be rendered
Change this
echo apply_filters( 'woocommerce_short_description', $product->post_excerpt );
to
return apply_filters( 'woocommerce_short_description', $product->post_excerpt );

Wordpress NextGen Remove "Set NextGEN Featured Image" From Edit Page

I'm using NextGen for some galleries and noticed that since loading this plugin, in the edit page area under the Featured Image Meta box there is now a link to "Set NextGEN Featured Image". I don't want to confuse the user (by having two "set featured image" links, so I'd like to remove the NextGEN option, leaving only the one default WP link to set the featured image.
I've found tutorials on how to change the text of the standard WordPress "Set Featured Image" Meta Box, but nothing on how to remove the NextGEN link, (I did find a post for adding a plugin to remove it: http://wordpress.org/support/topic/remove-set-nextgen-featured-image)
However, I would like to just remove it in my functions.php file (not use a plugin).
I've tried the following in my functions.php file:
remove_meta_box
remove_filter
remove_action
But I'm not 100% sure which I need to use (none have worked so far).
The file that is adding this function to the page edit area is: https://github.com/mneuhaus/foo/blob/master/nextgen-gallery/products/photocrati_nextgen/modules/ngglegacy/lib/post-thumbnail.php.
I realize that I would just comment out the text in this file that produces the link, but if I ever updated the plugin it would be overwritten.
Any help or suggestions are greatly appreciated! Thank you.
If you don't have/need anything customized for the Featured Image meta box, you can simply remove all filters:
function so_23984689_remove_nextgen_post_thumbnail_html() {
remove_all_filters( 'admin_post_thumbnail_html' );
}
add_action( 'do_meta_boxes', 'so_23984689_remove_nextgen_post_thumbnail_html' );
If there are any other filters, which you would want to keep, you have to loop through the filter array and remove the according element:
function so_23984689_remove_nextgen_post_thumbnail_html() {
global $wp_filter;
if ( ! isset( $wp_filter[ 'admin_post_thumbnail_html' ] ) ) {
return;
}
foreach ( $wp_filter[ 'admin_post_thumbnail_html' ] as $priority => $filters ) {
foreach ( $filters as $id => $filter ) {
if (
isset( $filter[ 'function' ] )
&& is_object( $filter[ 'function' ][ 0 ] )
&& $filter[ 'function' ][ 0 ] instanceof nggPostThumbnail
) {
unset( $wp_filter[ 'admin_post_thumbnail_html' ][ $priority ][ $id ] );
}
}
}
}
add_action( 'do_meta_boxes', 'so_23984689_remove_nextgen_post_thumbnail_html' );

Categories