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

WordPress – Character counter for the caption field of an image

MacBook avec code sur un bureau

For every WordPress image, we can add a caption or a title. It may be useful to add a counter. To do this, you must use both hooks admin_head-post.php and admin_head-post-new.php function cf_caption_count_js() { echo ‘<script type=”text/javascript”> jQuery(document).ready(function() { jQuery(“#attachment_caption”).wrap(“<div></div>”); jQuery(“#attachment_caption”).parent().css(“display”, “table”); jQuery(“#attachment_caption”).parent().css(“width”, “100%”); jQuery(“#attachment_caption”).after(“<span id=\’cf_caption_count\’ style=\’display:table-cell; width: 30px; margin-left: 5px; text-align: right;\’></span>”); jQuery(“#cf_caption_count”).html(jQuery(“#attachment_caption”).val().length); jQuery(“#attachment_caption”).keyup(function() … Read more

PHP – CURL Authentication

MacBook avec code sur un bureau

To authenticate a cURL request in PHP, the option to know is CURLOPT_USERPWD. It sends a username / password pair in the user:password format. Basic authentication $ch = curl_init(‘https://exemple.com/api’); curl_setopt($ch, CURLOPT_USERPWD, ‘utilisateur:motdepasse’); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); $reponse = curl_exec($ch); if ($reponse === false) { throw new RuntimeException(curl_error($ch)); } curl_close($ch); Setting … Read more

Categories PHP

jQuery File Upload – Basic solution with a callback

MacBook avec code sur un bureau

jQuery-File-Upload How to use the basic solution from jQuery-File-Upload and make it work on every browser, with a special dedication to Internet Explorer. The basic version has the advantage of not being overloaded with libraries, and above all it is much easier to import into a site that already exists. Libraries <script type=”text/javascript” src=”//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js”></script><script type=”text/javascript” … Read more