What is difference between these Wordpress filter hooks - wordpress

I'm new to WordPress plugin development. I have a doubt about below 3
filter hooks.
content_edit_pre
content_filtered_edit_pre
excerpt_edit_pre
Please tell me the what the difference between these hooks.

content_edit_pre filter hook
The content_edit_pre filter hook is used to hook into the content of a post just before it is loaded to be edited. For example, if you included the following at the end of your functions file:
function test_of_content_edit_pre( $content, $post_id ) {
return "Insert this before the content about to be edited ".$content;
}
add_filter( 'content_edit_pre', 'test_of_content_edit_pre', 10, 2 );
Then open a post to edit it, you would see that the text has been inserted before the post:
excerpt_edit_pre filter hook
The excerpt_edit_pre filter hook is very similar to content_edit_pre, except it is used to hook into excerpts (instead of posts) just before they are loaded to be edited. For example:
function test_of_excerpt_edit_pre( $content, $post_id ) {
return "Add this to excerpt".$content;
}
add_filter( 'excerpt_edit_pre', 'test_of_excerpt_edit_pre', 11, 2 );
Would result in this being shown in the excerpt:
content_filtered_edit_pre filter hook
This one I am not sure about. I tested it out and it didn't seem to do anything. I will update my answer if I can find more information on this.

Related

How to hide some Custom Fields from Woocommerce Admin order page

When logged in as an admin and looking at an Order in Woocommerce, there's a section with all the Custom Fields. Out of the whole list I only want it to display two of them. How do I hide the rest from this view? I don't want to delete them, but just hide from this view.
For every custom field you want hidden, add the following 4 lines of code to functions.php or using Snippets plugin:
add_filter('is_protected_meta', 'my_is_protected_meta_filter1', 10, 2);
function my_is_protected_meta_filter1($protected, $meta_key) {
return $meta_key == 'automatewoo_cart_id' ? true : $protected;
}
If you want to hide more than one, add the lines above again and change 'my_is_protected_meta_filter1' to 'my_is_protected_meta_filter2', etc
if you’re using ACF pro, there is a hook you can use to remove the field on the back end, but it’s not something that’s documented..
You could use a hook to remove specific field if is_admin() returns true.
You may need to play with this a bit to get it to work, the ACF hook is
acf/get_fields
So, for example:
add_filter('acf/get_fields', 'your_function_name', 20, 2);
function your_function_name($fields, $parent) {
// remove the fields you don't want
return $fields;
}
$fields can be a nested array of fields => sub_fields.
You need to set the priority > 10 to run after the internal ACF filter
For orders in Woocommerce the post type is 'shop_order', so your code should be:
add_action( 'add_meta_boxes', 'remove_shop_order_meta_boxe', 90 );
function remove_shop_order_meta_boxe() {
remove_meta_box( 'postcustom', 'shop_order', 'normal' );
}

how to remove "added-to-cart notice" from every page except product archive pages?

Note: In looking for an answer to my question I came across this post but it is NOT duplicate: Remove add to cart notice and change "add to cart" button in Woocommerce the answer there gives the option to remove the notice from the entire site. I want to remove it only from the cart page and I don't want to do it with CSS.
I use external links to my site to send people directly to the shopping cart with the item already added to the cart. When doing so, the "added-to-cart notification" shows up on the cart page which I do not want.
I found this code which removes the added-to-cart notification: add_filter( 'wc_add_to_cart_message_html', '__return_false' ); but it removes the notification from all pages of my site which is not what I want.
To be more specific, I want the added-to-cart notification to show on every product archive page and nowhere else.
I tried to add a filter but it doesn't work the way I would expect it to, I tried the following two ways (and tested it with various pages to see if I could make anything work but it seems my general syntax is off because I Can't get it to do anything...
function hide_cart_notes() {
if ( ! is_archive() ) {
add_filter( 'wc_add_to_cart_message_html', '__return_false' );
}
}
add_action( 'woocommerce', 'hide_cart_notes' );
function hide_cart_notes() {
if ( is_archive() ) {
return;
}
add_filter( 'wc_add_to_cart_message_html', '__return_false' );
}
add_action( 'woocommerce', 'hide_cart_notes' );
when woocommerce hook starts? where it's docs? does it run at all?
these question should be answered before.
i know that WordPress parses query at parse_query hook, so i would try this
add_action('parse_query', function() {
if (!is_archive()) {
add_filter( 'wc_add_to_cart_message_html', '__return_false' );
}
});
because is_shop(), is_archive(), is_* need query to be parsed first.

Wordpress: Job Manager: How to modify Resume custom fields?

I have been trying to figure out a filter or action to modify Resume custom fields.
There is a documentation here about how to do it for resume core fields but not for custom fields.
If I use submit_resume_form_fields filter like
add_filter( 'submit_resume_form_fields', 'remove_submit_resume_form_fields' );
function remove_submit_resume_form_fields( $fields ) {
$fields only returns resume core fields but not the custom fields.
Can anyone help me?
Finally I got it worked. So instead of using submit_resume_form_fields filter, I used submit_resume_form_fields_get_resume_data and it gave me all the fields (with custom fields as well). Unfortunately I could not find a documentation of this, I had to search the code. So the code goes like
add_filter( 'submit_resume_form_fields_get_resume_data', 'remove_submit_resume_form_fields' );
function remove_submit_resume_form_fields( $fields ) {
unset( $fields['resume_fields']['custom_field_name'] );
}
I thought it might help someone!

how can I detect that option value has changed in wordpress?

I am working on a Wordpress plugin and I want to fire a different action based on each option value change. so how can I do that ?
Example:
$options = get_option('ACP_settings'); if $options['acp-select'] has changed from previous value than I want to fire different action based on selection
Please note that $option['acp-select'] is retrieved from select/option html form and has following values for selection:'book','air','SW','HW' and etc....
I hope I have posted my question clearly.
thanks for help
You can use wordpress hooks in the update_option() function.
In your case you can use updated_option or update_option_{$option} hooks.
The code for ACP_settings option would look like:
add_action( 'update_option_ACP_settings', function($old_value, $value, $option){
if( $old_value !== $value ){
// Your option was updated
// Run your code here
}
}, 10, 3 );

execute do_shortcode in functions.php

I'm trying to run a do_shortcode in functions.php with no luck
I'm using Types plugin http://wp-types.com/ for creating custom post types and custom fields.
What I'm trying do is adding a custom column in admin for view all custom posts that displays a thumbnail from a custom field.
This is what I got so far, but it seems that the shortcode doesn't work inside functions.php
// add a column for custom post type (products)
add_filter('manage_product_posts_columns', 'add_thumbnail_column');
add_action('manage_product_posts_custom_column', 'add_thumbnail_content', 10, 2);
function add_thumbnail_column($defaults)
{
$newSlice = array('thumbnail' => 'Image preview');
$counter = 2;
$array_head = array_slice($defaults,0,$counter);
$array_tail = array_slice($defaults,$counter);
$defaults = array_merge($array_head, $newSlice);
$defaults = array_merge($defaults, $array_tail);
return $defaults;
}
function add_thumbnail_content($column_name, $post_ID)
{
// this one works when putting into post content
echo do_shortcode('[types field="square-picture" id="' . $post_ID . '" size="thumbnail"]' . '[/types]');
}
Can anyone help please?
In the Wordpress notes for the function it says
"If there are no shortcode tags defined, then the content will be
returned without any filtering. This might cause issues if a plugin is
disabled as its shortcode will still show up in the post or content."
Types may be conditionally declaring their short code only when you are on the frontend. What may be happening is that, in the admin, the short code is not defined and you are simply getting a false return. While on the frontend, the shortcode is defined and you get the results you intended.

Resources