updated_{$meta_type}_meta not firing, but updated_post_meta is - wordpress

I'm not sure if I'm using it correctly, but I can't get the updated_{$meta_type}_meta hook to work. There is a updated_post_meta hook which runs when you save a posts meta (and possibly other times, I haven't checked). I can't find much reference to updated_{$meta_type}_meta apart from here, so I don't really understand if I am even hooking it correctly, because I didn't read it properly at first and so thought it should be used like: updated_CPT_meta, but that didn't work, so I tried a meta key instead of the CPT.
My question is, what should $meta_type be ?

Of course I found this straight after I posted
As the page at https://core.trac.wordpress.org/browser/tags/3.8.1/src/wp-includes/meta.php#L0 states, $meta_type Type of object metadata is for (e.g., comment, post, or user).
So, you should just use it as updated_post_meta for any CPTs also.
duh.

Related

Woocommerce Underscore is added to some product URLs

Got an unusual problem that started recently and wonder if anyone has any ideas why it's happening and how to stop it.
Some of my products are getting an underscore added to the url when selected which, naturally, results in a 404 error. It's consistent - either an underscore gets added or it doesn't. It doesn't seem to be related to the GUID or the post_name in the wp_posts table. It also doesn't matter if you call it using the URL or the post ID so the following:-
Bad index.php?page_id=1075707 the-john-rutter-christmas-piano-album-2 https://website-name.com/product/the-john-rutter-christmas-piano-album-2/
Good index.php?page_id=1087442 the-john-rutter-piano-album https://website-name.com/product/the-john-rutter-piano-album/
Bad index.php?page_id=1159681 parable-for-harpsichord https://website-name.com/product/parable-for-harpsichord/
Good index.php?page_id=1159684 jesu-meine-freude-2 https://website-name.com/product/jesu-meine-freude-2/
Bad here meaning that the URL appears with an underscore such as
https://website-name.com/product/parable-for-harpsichord_/
I've also tried this with every single plugin, except WooCommerce, de-activated and with my functions.php disabled. No difference.
I've tried using the URLs as given above, C&P or typed in manually and by directly referencing the post ID (as shown).
This has only recently started and I'm at a bit of a loss here. I don't know how widespread this problem is either since I've over 120,000 products and can't check them all by hand. Fortunately, this isn't live yet.
Any suggestions or ideas here would help me.
There appears to be a hidden character at the end of the post_name field. Deleting and recreating the field got rid of the problem.

Is values passed to WooCommerce update_meta_data method fully sanitizing?

I'm writing a WordPress plugin in which there is an input form for users with which they can add notes. I'm using WooCommerce update_meta_data method to save notes in database.
Considering this code:
$note = isset($_POST['order_note']) ? sanitize_text_field($_POST['order_note']) : '';
$order->update_meta_data('_order_note', wp_json_encode($note));
$order->save_meta_data();
I know that update_post_meta sanitizes data (SQL Injection) before inserting it into database but how about update_meta_data ?
Is above code safe to use for inserting data in database?
update_meta_data
As far as I can see on the woo's update_meta_dataSource Code, there is not any sanitizing function getting called.
update_metadata
On the other hand, if you take a look at the wordpress update_metadataDocs, there are two sanitizing functions getting called:
sanitize_keySource Code
and
sanitize_metaSource Code
So to answer your question, yes I would use a sanitizing function too before I update the meta data using update_meta_data.
In order to do that, sanitize_text_field would usually get the job done fine, but if you want to be sure that you're using the right sanitizing function, then use sanitize_metaDocs instead. That's what wordpress itself is using. Security-wise, I, personally, never had any problems using sanitize_text_field nor did I see anybody else having any problems with it. The snippet you provided us with, looks safe to me.

User access to CPT (Custom Post Type) without access to regular posts

I have created a custom post type that for the sake of this question we can call "my_cpt".
I have also created a new role which we can call "my_role".
What I want to do is give "my_role" access to "my_cpt" without adding the "edit_posts" capability because that gives access to other post types which I do not want the role to have access to.
I have tried variations of numerous bits of code I have found but none have worked so I don't really know where to begin. Based on that I don't have any base code to display here.
A plugin like this one should enable you to do this: https://www.role-editor.com/
Ended up being easier than I thought.
I created a couple capabilities... "read_my_cpt" and "edit_my_cpt".
The "my_role" has permissions to both the capabilities.
I then just had to change the capability_type for the CPT to "my_cpt" instead of "post"

Using attribute from "parent"

Let's suppose I have Posts and Comments collections. A Comment record contains the attributes comment and postId. A Post record has a title attribute.
Now I have a separate page for a single comment (very unlikely irl, but just for the sake of it) where I want to show the comment of course but also the Post title. How would I do this, or more in general: how do you reference an attribute from a "parent" (parent between quotes because Mongo is not a referential database)?
One option I often see is to denormalize and also add the Post title as attribute to the Comment record. Although I do see use cases for denormalization, I am worried about the maintenance nightmare. When the Post title gets update, you also need to update them in all other collections where it is used as denormalized attribute.
So, my solution would be to subscribe also to the Post on the Comment page. Then in commentDetails.html I have this:
{{post_title}}
And in commentDetails.js I would have this:
Template.commentDetails.helpers({
post_title: function() {
return Posts.findOne({_id: this.postId}).title;
}
});
Is this the way to do this?
Edit: clarified the question and updated the example.
After being pointed on Reddit to a detailed article about this subject, I have implemented as the "naive approach" for now. I am looking into option 4 to make it really reactive.

Wordpress Loop get_the_id()

I tried the following functions in header.php, footer.php, single.php etc.
var_dump(in_the_loop());
var_dump(get_the_id());
The first function gives false (meaning we are not in the loop) and the second function gives the post id every single time.
Description of get_the_id() from wordpress :
Retrieve the numeric ID of the current post. This tag must be within The Loop.
I just want a simple explanation what the hell is going on why do i get the post id if I call the function out of the loop !?
must is a little strong for get_the_id() ...delivers evil eye to Wordpress.
It works in the header and non-loop (confirmed).
Please note that post/page are essentially interchangeable in this conversation.
Think of WP this way -> You always have a post id in some way, all the time, every page, unless you do weird stuff or talk about non-page edge cases. When you are at the install root (such as site.com/) there are posts being called, something has to be displayed. There are other settings that will impact post/page such as static front page settings. On a category listing, if there are pages, I got the first ID returned before the loop.
On post/pages the page ID is (more or less0 set before the loop. This is a result of the URL (pretty or ?p=123 format) dictating the content. Using pretty names, the page at site.com/foo-bar/ will try to look up if there is content available via the permalink rules for "foo-bar". If there is content, the post ID is obtained. (simplified)
Later in the page build you get into the loop. However, before the loop you are also offered opportunities to change, sort, or augment the loop - such as changing the page IDs to be looped or sorting.
Regarding in_the_loop(), WP says
"True if caller is within loop, false if loop hasn't started or has ended." via http://codex.wordpress.org/Function_Reference/in_the_loop
in_the_loop() evaluates if the loop is in action (loop being key to the WP world). Also important - when you are in the loop WP can iterate over multiple page/post (IDs).
I don't have a 100% bulletproof explanation as to how the ID always shows, but when you dig into the API and various methods for hooking this might be a result.
I understand your confusion and agree with you. I think WP intended get_the_id() as a loop based tool, outside the loop you will get unpredictable results.
Hope that helps, I do enjoy working in WP, and I hope you do to.

Resources