Shortcode attributes without values and with special characters - wordpress

Can Wordpress shortcodes have attributes without values like this?
[foo some_att]
I know it can be like this:
[foo some_att=""]
Can I have shortcode values like this (with curly braces)?
[foo path="{'quality': '720p', 'mp4': 'PATH_TO_MP4_VIDEO'}"]

You sure can! RichJenks has a short yet informative article on implementing this functionality, which boils down to defining a new function that can tell you if a flag (argument with no associated value) is present in your $atts array:
function is_flag( $flag, $atts ) {
foreach ( $atts as $key => $value )
if ( $value === $flag && is_int( $key ) ) return true;
return false;
}
After defining this function, you can reference is_flag() in your shortcode function to tell if the flag has been provided as part of the shortcode invocation:
if is_flag( 'some_att', $atts )
// flag is present
else
// flag is not present
Unfortunately, the example you've provided wouldn't work as an argument value with the Wordpress shortcode parser. According to the official Wordpress documentation on the Shortcode API:
Attribute values must never contain the following characters:
Square braces: [ ]
Quotes: " '
Since your example makes use of the single-quote character ('), it is officially unsupported by the shortcode parsing engine.

You can use this for first question.
is_int( array_search( 'some_att', $atts ?: [] ) )

Related

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.

Contact Form 7 Regular Expression Validation

I'm trying to add regular expression validation to Contact Form 7 'last-name' field that will allow for hyphenated names. I have researched and written a function to allow for this, but it doesn't seemed to be working. Any help would be appreciated.
Here is the function I have written and placed in functions.php file...
add_filter('wpcf7_validate_text', 'custom_text_validation', 20, 2);
add_filter('wpcf7_validate_text*', 'custom_text_validation', 20, 2);
function custom_text_validation($result, $tag) {
$type = $tag['type'];
$name = $tag['name'];
if($name == 'last-name') {
$value = $_POST[$name];
if(!preg_match('[a-zA-Z\-]', $value)){
$result->invalidate($tag, "Invalid characters");
}
}
return $result;
}
So the first thing I think we will need to look at it is on your 5th and 6th lines. According to the CF7 documentation, the $tag argument actually returns an object and not an array.
Which means that $tag['name'] and $tag['type'] should actually be $tag->name and $tag->type.
The second thing to address is your regex expression, now would be a good time to read up on Falsehoods Programmers Believe about Names. Basically, in short, there are a lot of last names that will not match if the criteria is MixedAlpha and a dash.
If, however, you are intent on cutting out a portion of potential users, might I suggest making use maček's basic regex listed on this SO answer as it will atleast include a few more potential valid last names.
This would turn your function into something like this:
add_filter('wpcf7_validate_text', 'custom_text_validation', 20, 2);
add_filter('wpcf7_validate_text*', 'custom_text_validation', 20, 2);
function custom_text_validation($result, $tag) {
$type = $tag->type; //object instead of array
$name = $tag->name; //object instead of array
if($name == 'last-name') {
$value = $_POST[$name];
if(!preg_match("/^[a-z ,.'-]+$/i", $value )){ //new regex statement
$result->invalidate($tag, "Invalid characters");
}
}
return $result;
}
Try negating it
if(preg_match('/[^a-z\-]/i', $value)){
I've also updated it to use /i, which will ignore case

Trying to get lowest priority number in WordPress

Hello I am trying to get lowest priority number from all the functions that hook into wp_head.
My code is this.
function wpcrown_wpcss_loaded() {
global $wp_filter;
$lowest_priority = max(array_keys($wp_filter['wp_head']));
add_action('wp_head', 'wpcrown_wpcss_head', $lowest_priority + 1);
$arr = $wp_filter['wp_head'];
}
add_action('wp_head', "wpcrown_wpcss_loaded");
But its shown error like this.
Warning: array_keys() expects parameter 1 to be array
Warning: max() [<a href='function.max'>function.max</a>]: When only one parameter is given, it must be an array in
Please help to solve this.
Thanks
As Benoti said $wp_filter is no longer an array so array_keys is throwing these warnings because expects an array.
If you want to add scripts / styles / whatever you have in wpcrown_wpcss_head at the end of the head (to give priority) this should work:
1) Comment this action:
// add_action('wp_head', "wpcrown_wpcss_loaded");
2) Use this one instead:
add_action('wp_head', "wpcrown_wpcss_head");
Hope it helps.
Be carefull on the use of $wp_filters as an array since WordPress 4.7,
Here is the guidelines for wp .4.,7
Make.wordpress that explain:
If your plugin directly accesses the $wp_filter global rather than using the public hooks API, you might run into compatibility issues.
Case 1: Directly setting callbacks in the $wp_filter array
$wp_filter['save_post'][10]['my_special_key'] = array( 'function' => 'my_callback_function', 'accepted_args' => 2 );
This will no longer work, and will cause a fatal error. $wp_filter['save_post'] is no longer a simple array. Rather, it is an object that implements the ArrayAccess interface.
You have two options to work around. The first (and preferred) method is to use the add_filter() or add_action() functions.
add_action( 'save_post', 'my_callback_function', 10, 2 );
If, for some reason, you absolutely cannot, you can still work around this.
if ( ! isset( $wp_filter['save_post'] ) ) {
$wp_filter['save_post'] = new WP_Hook();
}
$wp_filter[ 'save_post' ]->callbacks[10]['my_special_key'] = array( 'function' => 'my_callback_function', 'accepted_args' => 2 );
Case 2: Directly unsetting callbacks in the $wp_filter array
unset( $wp_filter['save_post'][10][ $my_callback_id ] );
This will fail for the same reason as case one. To work around, you can use the standard remove_filter() / remove_action() functions.
remove_action( 'save_post', 'my_callback_function', 10, 2 );
Or, if you absolutely must access the array directly:
if ( isset( $wp_filter[ 'save_post' ] ) ) {
unset( $wp_filter['save_post']->callbacks[10][ $my_callback_id ] );
}
Case 3: Checking if a hook is an array
if ( isset( $wp_filter['save_post'] ) && is_array( $wp_filter['save_post'] ) ) {
// do something
}
This will always return false. $wp_filter['save_post'] is a WP_Hook object. To check if a hook has callbacks, use has_action() or has_filter()
if ( has_action( 'save_post' ) ) {
// do something
}
Case 4: Moving the $wp_filter array pointer manually
If you’re calling next() or prev() on the $wp_filter array pointer to manually manage the order that callbacks are called in (or if you’re doing it to work around #17817), you will likely be unsuccessful. Use callback priorities, add_action() / add_filter(), and remove_action() / remove_filter() appropriately and let WordPress manage execution order.

WPDB Wordpress Strip Slashes (Remove Slashes)

I use WPDB Class in Wordpress for my Data within my theme...
Now lets say in the database we have saved
This isn\'t my name
Now, I want to remove the slash for the complete fields using the following method
$query= 'SELECT * FROM quotes';
$quotes= $wpdb->get_results($sql);
Is there a way how I can do something recursive for all fields in $quotes that will practically to stripslashes (PHP) and return me something like (for all fields within the object)
This isn't my name
I think what you want is stripslashes_deep.
Since the $wpdb->get_results() funciton returns an array of stdClass objects, in order to remove the slashes in all columns in every row, you must iterate through the rows, and through the properties of each row object running the PHP stripslashes() function on it.
foreach( $quotes as &$quote ) {
foreach( $quote as &$field ) {
if ( is_string( $field ) )
$field = stripslashes( $field );
}
}
More information on the wpdb->get_results() function:
http://codex.wordpress.org/Class_Reference/wpdb#SELECT_Generic_Results

Shortcode Attribute Containing Bracket Issue

The Shortcode API states that you can't have square brackets in your attribute. Thus, the following would not work:
[tag attribute="[Some value]"]
In my case, the square bracket is required. What would be the best solution of getting around this problem? I've already tried escaping the content in my shortcode function, but had no luck.
I am using WordPress 3.3.1.
Use some other special character in your shortcode and replace it square brackets in your shortcode function. - Since this is not what you want, here's an alternative.
This seems to be the only thing that I can think of that will work in your case, instead of relying on the Shortcode API, you can use "apply_filters" on the content, and then use preg_replace to write your own shortcode processing function.
If the brackets appear as part of the HTML generated try to use &#...; or as part of an URL use %... .
Otherwise, if it concerns your own shortcode, just replace some other character, e.g. {} by [], inside the code of the shortcode.
If it's not your own shortcode, you might modify the plugin / core. I'd write a wrapper code in order not to break updates.
We store json in shortcode attributes. We decided to use base64_encode to hide square brackets, but we faced some problems:
Themecheck complains about base64 functions
When you want to make some replace in database, for e.g. to replace all old site urls with new site url, the regex can't see what's inside a base64 encoded string
There is another solution, using htmlentities
function encode($str) {
$str = htmlentities($str, ENT_QUOTES, 'UTF-8');
// http://www.degraeve.com/reference/specialcharacters.php
$special = array(
'[' => '[',
']' => ']',
);
$str = str_replace(array_keys($special), array_values($special), $str);
return $str;
}
function decode($str) {
return html_entity_decode($str, ENT_QUOTES, 'UTF-8');
}
$original = '[1,2,3,"&",{a:1,b:2,"c":"Результат"}]';
$encoded = encode($original);
$decoded = decode($encoded);
echo "Original:\t", $original, PHP_EOL;
echo "Shortcode:\t", '[hi abc="'. $encoded .'"]', PHP_EOL;
echo "Decoded:\t", $decoded, PHP_EOL;
echo "Equal:\t\t", ($original === $decoded) ? 'YES' : 'NO';
Output
Original: [1,2,3,"&",{a:1,b:2,"c":"Результат"}]
Shortcode: [hi abc="[1,2,3,"&",{a:1,b:2,"c":"Результат"}]"]
Decoded: [1,2,3,"&",{a:1,b:2,"c":"Результат"}]
Equal: YES
http://ideone.com/fNiOkD

Resources