WordPress – how to retrieve the ID from a given meta

Code sur écran d'ordinateur

With WordPress, it is common to use the get_post_meta($post, $meta_key) method in order to retrieve the value of a given meta. However for the ID, there is no ready-made method. The best solution I’ve found so far: $mid = $wpdb->get_var( $wpdb->prepare(“SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s”, $post, $meta_key) );

Categories PHP

PHP – Protection of special characters of regular expressions

Code sur écran d'ordinateur

If you want to use a regular expression to, for example, search for a word in a paragraph, it is important to protect your variable. To do this, just use the following function: string preg_quote ( string $str [, string $delimiter = NULL ] ) For example: foreach ($aRequest[‘keywords’] as $sKeyword) { $sKeyword = preg_quote($sKeyword); … Read more

Categories PHP

JavaScript – Iframe – the address where the iframe is located

MacBook avec code sur un bureau

For security reasons, you can’t directly reach the page outside the iframe from inside it. However, we can retrieve information from the browser. To retrieve the parent’s address, i.e. the URL of the page where the iframe is added, just write this: <script type=”text/javascript”> var url (window.location!) – window.parent.location)? document.referrer: document.location; alert (url); </script> This … Read more

jQuery – Word and Character Counter

MacBook avec code sur un bureau

A small script to count the number of words and/or characters in an input or textarea field <input id="text" name="text" type="text"> <span id="count"></span>"; We place the script in jQuery’s ready function to make sure the HTML has been fully loaded. <script></script> (document).ready (function) count ($("#text"),$("#count") $("#text"). count ($("#text"),$("#count") }); }); function count (src,dest) var txtVal … Read more

Categories 431

PHP – mb_strlen: get the real length of a string

MacBook avec code sur un bureau

A lot of code uses strlen to get the size of a string. The problem: strlen returns the number of bytes, not the number of characters. As soon as a string contains accented letters or emojis, the result is wrong. The problem with strlen In UTF-8, a character such as “é” takes two bytes, so … Read more

Categories PHP

PHP – Replace accented characters with their unaccented equivalents

MacBook avec code sur un bureau

A small function to replace accented characters with their unaccented equivalent. function normalize_str($str) { $invalid = array(‘Š’=>’S’, ‘š’=>’s’, ‘Đ’=>’Dj’, ‘đ’=>’dj’, ‘Ž’=>’Z’, ‘ž’=>’z’, ‘Č’=>’C’, ‘č’=>’c’, ‘Ć’=>’C’, ‘ć’=>’c’, ‘À’=>’A’, ‘Á’=>’A’, ‘Â’=>’A’, ‘Ã’=>’A’, ‘Ä’=>’AE’, ‘Å’=>’A’, ‘Æ’=>’A’, ‘Ç’=>’C’, ‘È’=>’E’, ‘É’=>’E’, ‘Ê’=>’E’, ‘Ë’=>’E’, ‘Ì’=>’I’, ‘Í’=>’I’, ‘Î’=>’I’, ‘Ï’=>’I’, ‘Ñ’=>’N’, ‘Ò’=>’O’, ‘Ó’=>’O’, ‘Ô’=>’O’, ‘Õ’=>’O’, ‘Ö’=>’OE’, ‘Ø’=>’O’, ‘Ù’=>’U’, ‘Ú’=>’U’, ‘Û’=>’U’, ‘Ü’=>’UE’, ‘Ý’=>’Y’, ‘Þ’=>’B’, … Read more

Categories PHP

WordPress – How to run a LIKE query with WP_Query

MacBook avec code sur un bureau

To make queries on WordPress, I’m used to using WP_Query($args). In this example, we want to add a LIKE = “%title%” in our query. Here is a simple request that adds all our posts in the array a_response_array: $args = array( ‘post_status’ => ‘publish’, ); // The Query $query = new WP_Query( $args ); // … Read more