strpos() on title in a metabox in wordpress fails to find '-' - wordpress

I was coding a simple metabox for wordpress and have a little issue when saving data.
The meta box is in my "create article"-page has two textfields. These are saved as post-meta, when the post is saved.
While saving I check if the fields were filled - if they are empty I take the post title and extract the data I need. The idea is to take everything that is before the first "-". If there is no minus sign, the whole title should be saved in my custom field. Now, this fails to find "-" in the title (alltough there is one) and returns the whole title every time:
function get_from_title($title) {
$pos = strpos($title, '-');
if ($pos) {
return trim(substr($title, $pos));
}
else {
$pos = strpos($title, '–'); //added this since two different signs could be used
if ($pos) {
return trim(substr($title, $pos));
}
else {
return $title;
}
}
}
the function that is calling get_from_title is getting the title via get_the_title( $post_id ) and this works without problems.
Is wordpress encoding the title somehow? Why can't strpos find the minus sign? What should I look for instead?
Thanks

I replied to the thread you started on this topic in the WordPress forums. You can find your answer there.
Alternatively, here's what I said. :)
Ah yes. This is a tricky one. So, why can't strpos find a hyphen in
the title when clearly we can see one? Because there isn't one. hehe.
What WordPress is doing here is converting your hyphen ( minus sign )
into an en-dash.
This will give you diddly-squat:
$pos = strpos( $title, '-' );
You want this:
$pos = strpos( $title, '–' );
Let me know how things turned out for you. :)

Related

auto generate post title in wordpress like ABC-123456

I want to auto-generate post title like that
Ex : ABC-123456
also i need to let (( ABC- )) fixed and random change the 06 numbers
and to dont change the post title through updating the post
First, to modify WordPress behavior the correct way, you find an appropriate hook. In this case, that would be a filter that allows changing the Post data before it is saved to the db.
The filter 'wp_insert_post_data' is exactly what you need, so you add your filter, and connect it to a function like so:
function zozson_filter_post_title(){
}
add_filter( 'wp_insert_post_data', 'zozson_filter_post_title',50,4);
'wp_insert_post_data' is the name of the filter
'zozson_filter_post_title' is the name you give to your function, to hook to it.
50 is the priority. I chose 50 to run it after most other things. Default is 10
4 is the number of variables that the filter passes to your function.
So now we will add those variables and the logic inside it, to assign these CPT sho7nat those titles on admin saving them.
function zozson_filter_post_title( $data, $postarr, $unsanitized_postarr, $update){
//Then if it is the post type sho7nat
if( $data['post_type'] !== 'sho7nat' ){
return $data;
}
//If there is already a titled saved ($update returns true always)
if( $data['post_title'] !== '' ){
return $data;
}
//Let's build our title
$post_title = ' ABC-';
//What better random number that a unique timestamp?
$random_number = strtotime('now');
//Add the random number to the post title to save. You can do these in 1 line instead of 3
$post_title.= $random_number;
//We now have a post title with ABC- fixed and a random number, tell WordPress to use it as the post title
$data['post_title'] = $post_title;
return $data;
}
add_filter( 'wp_insert_post_data', 'zozson_filter_post_title',50,4);
The title automatically assigned should be like in this example:

Woocommerce : How to add a custom meta_key to checkout billing_phone?

I want to enter the value of digits_phone meta key to be entered as billing_phone for every woocommerce order.
I came up with something like this but it did not work :
//Automatically add the digits phone number of the user, to woocommerce orders for every order
add_filter('woocommerce_checkout_posted_data', 'dg_manipulate_checkout_posted_data');
function dg_manipulate_checkout_posted_data ($data) {
$data['billing_phone'] =['digits_phone'];
return $data;
}
can anyone please help me to figure this out?
I have never used the plugin myself, take this as a guideline
THIS CODE IS NOT TESTED !
Maybe this question is a better fit for wordpress.stackexchange.com
From the last comment of this post I see that there should be 2 user metadata related to some digits_phone: 'digits_phone' and 'digits_phone_no'
Assuming that the one we want is digits_phone, this code should be a hint in the right direction:
add_filter('woocommerce_checkout_posted_data', 'dg_manipulate_checkout_posted_data');
function dg_manipulate_checkout_posted_data ($data) {
// save current user data in variable $current_user
$current_user = wp_get_current_user();
//get digits phone from db and save in variable $digits_phone
$digits_phone = get_user_meta( $current_user->ID, 'digits_phone' , true );
// assign to POSTed array and return it
$data['billing_phone'] = $digits_phone;
return $data;
}
Also have a look at How can I convert woocommerce checkout fields in capital letters to get a better picture of manipulating POST data

WP Rest API get post by slug with special characters (ex: !&')

I have posts with slugs with special characters. One of them is the following:
http://localhost/wp-json/wp/v2/posts?slug=my-post!
Unfortunately, WP REST API not showing the content since it has (!) within the slug.
Is there any solution you guys would recommend?
I have found the solution (at least for my case), not sure if it'll work for you but may indicate you the way to go. We need to tap into the function used to sanitize the slugs, as I said in my comment, by default is wp_parse_slug_list. Looking at the code the function that actually sanitizes the slug is sanitize_title.
Looking at the source code, wp_parse_slug_list calls sanitize_title with only one argument, which means that the context used is save. This means that, for posts that were already saved without being sanitized by this function, the slug won't match and the post will be inaccessible through the API. The solution is to change the sanitizing function slightly by adding a filter:
add_filter('rest_post_collection_params', function($query_params) {
$query_params['slug']['sanitize_callback'] = 'sanitize_rest_api_slug';
return $query_params;
}, 20, 1);
function sanitize_rest_api_slug( $list ) {
if ( ! is_array( $list ) ) {
$list = preg_split( '/[\s,]+/', $list );
}
foreach ( $list as $key => $value ) {
$list[ $key ] = sanitize_title( $value, '', 'query' );
}
return array_unique( $list );
}
The filter is actually being applied on the function get_collection_params() on the class-wp-rest-posts-controller class, but if you look at the source code, the filter has a variable depending on the post_type, so if you have another special kind of posts defined (besides post) you need to add/change a filter for that kind as well.
I hope this helps somebody else like me, even if it's too late for your issue.

Username only lowercase and numbers in WP multi?

We are using wordpress in multi sub-domains at work.
So we have administrator for each domain who can create users for subscribing editing contributing.
The problem is, our company edit the rule that all logins/usernames have to be like this "first-name.family-name" ex "barack.obama"... :-)
But the administrator can not do it, only super admin can do it !
In sub-domain administrator mode, there is a message that says : "Only the lowercase letters a-z and numbers allowed".
So we want to make it possible to admin to create users with first-name.family-name, but we try to search how to change it in the code, without finding it.
If someone can help, thanks !
In multisite usernames are validated with wpmu_validate_user_signup, which you can find in /wp-includes/ms-functions.php#L462.
You can add a dot there, after the 0-9, which is not recommended, since you will then hate to update WordPress core.
if ( $user_name != $orig_username || preg_match( '/[^a-z0-9.]/', $user_name ) ) {
So, a better solution would be to create a plugin for it.
You could try to add the following in a php-file and add it to /wp-content/mu-plugins/
<?php
/*
Plugin Name: wpmu no username error
*/
function wpmu_no_username_error( $result ) {
$error_name = $result[ 'errors' ]->get_error_messages( 'user_name' );
if ( empty ( $error_name )
or false===$key=array_search( __( 'Only lowercase letters (a-z) and numbers are allowed.' ), $error_name)
) {
return $result;
}
// only remove the error we are disabling, leaving all others
unset ( $result[ 'errors' ]->errors[ 'user_name' ][$key] );
/**
* re-sequence errors in case a non sequential array matters
* e.g. if a core change put this message in element 0 then get_error_message() would not behave as expected)
*/
$result[ 'errors' ]->errors[ 'user_name' ] = array_values( $result[ 'errors' ]->errors[ 'user_name' ] );
return $result;
}
add_filter( 'wpmu_validate_user_signup', 'wpmu_no_username_error' );
I didn't try it though. This is based on "Changing the username character limit from four to less characters".
I hope this helps. At least it should get you close. GL with it!

Get three posts before a certain date in Wordpress

I was wondering how I can get the last three posts before (and after) a certain date.
I was looking at query_post but I can't figure it out.
I'm looking to do this in functions.php. Ideally, it would return just basic home page stuff.. title, first image, num_comments, etc. I don't need to query the whole article.
EDIT: I would also like this all done in one function.. there are explanations out there on how to add a filter function.. which I don't want to do. If the functionality could be simply placed in add_filter(...) that would be fine.
Any help would be very much appreciated!
Matt
Got'er. Uses AJAX. Thank you for being a baller PHP (inner functions).
function ajax_get_next_posts($wp) {
if(!$_POST['date']) return 'error';
function filter_where($where = '') {
$where .= " AND post_date <= '{$_POST['date']}'";
return $where;
}
add_filter('posts_where', 'filter_where');
$posts = query_posts('posts_per_page=3');
print_r($posts);
}

Resources