PHP – How to find the origin of the call on a PHP function

Code sur écran d'ordinateur

It can happen that a function is only callable from one interface (console, apache…). There is a function that returns exactly what we are looking for. In the following example, we are looking to know whether the call is from the console. if (php_sapi_name() != “cli”) { header(“Location: http://www.sadtrombone.com/”); die(); }

Categories PHP

WordPress – How to add or remove a TinyMCE button

MacBook avec code sur un bureau

By default on WordPress we have TinyMCE and although this plugin is very powerful, there are far too many buttons. Fortunately with a few lines of code, it is possible to configure it. In your theme’s functions.php file or in your plugin, use the hook as follows: function configure_mce( $init ) { // nos options … Read more

JavaScript – How to add jQuery from a JavaScript file

Lignes de code HTML

When you create a javascript file, you may want to make sure you can use jQuery. To avoid having it twice and maybe introducing incompatibilities, just add this at the beginning of the javascript file: if (typeof jQuery == ‘undefined’) { var script = document.createElement(‘script’); script.src = ‘http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js’; script.type = ‘text/javascript’; document.getElementsByTagName(‘head’)[0].appendChild(script); }

PHP – CodeIgniter how to create and use a model

MacBook avec code sur un bureau

Looking at the structure of CodeIgniter we see that there is a folder called application/models. It is in this one that we will create our model. For a model to be usable as such, it must extend CI_Model. class ScoresModel extends CI_Model { function __construct(){ // Call the Model constructor parent::__construct(); $this->load->database(); } function getAllScores(){ … Read more

PHP – Create a REST API with CodeIgniter

Code sur écran d'ordinateur

Introduction CodeIgniter is a fairly simple framework for PHP. Compared to its big brothers such as Zend or Yii, it has the advantage of being small and very simple to get to grips with. For me, currently the best for small projects such as creating a REST-API. Installation To start, go download the official version … Read more

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: 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); … Read more