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);

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: preg_quote thong ($str thon[, string $delimiter = NULL ]g) For example: foreach ($aReque[‘keywords’]st as $sKeyword) $sKeyword – preg_quote ($sKeyword); $pattern ' ' … Read more

PHP – mb_strlen to regain the size of a character chain and not strlen

MacBook avec code sur un bureau

By default I’ve seen a lot of code using strlen to retrieve a character string size. Unfortunately this method returns the number of bytes used rather than the number of characters. That is, depending on your encoding, you’re going to have differences of +/- 1 character. To avoid this, you could do: strlen (utf8_encode($string)) But … Read more

PHP – Replace accents with their counterparts.

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', ''Z', ''Z', ''Z', ''Z', ''Z', ''Z', ''Z', ''Z', 'Z', 'C', ''C', ''C', ''C', ''C', ''A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'EA', ''A', ''A', ''A', ''C', ''E', 'E', 'E', 'E', … Read more

PHP – CURL Authentication

MacBook avec code sur un bureau

To make a curl request with authentication, just add the CURLOPT_USERPWD option $ch curl_init(); curl_setopt ($ch,CURLOPT_URL, url'); curl_setopt ($ch, CURLOPT_USERPWD, "user:pswd"); curl_setopt ($ch,CURLOPT_CONNECTTIMEOUT.5); curl_setopt ($ch,CURLOPT_RETURNTRANSFER,1); $buffer – curl_exec ($ch); curl_close ($ch); if (empty($buffer)) print "Need to recover from this! <>"; } Else print "There was data returned using curl. <>"; print "Buffer content -$buffer." <>"; … Read more