Skip to content

Instantly share code, notes, and snippets.

View imelgrat's full-sized avatar

Iván Melgrati imelgrat

View GitHub Profile
@imelgrat
imelgrat / wordcount.php
Last active August 23, 2017 18:11
Calculate the number of words in a Wordpress post. Full article at: http://imelgrat.me/wordpress/customize-wordpress-post-management-page/
function post_word_count($post_id) {
$content = get_post_field( 'post_content', $post_id );
$word_count = str_word_count( strip_tags( strip_shortcodes($content) ) );
return $word_count;
}
@imelgrat
imelgrat / show-wordcount.php
Last active June 22, 2018 08:05
Display posts word-count column in Wordpress post manage page: Full article at: http://imelgrat.me/wordpress/customize-wordpress-post-management-page/
function post_word_count($post_id) {
$content = get_post_field( 'post_content', $post_id );
$word_count = str_word_count( strip_tags( strip_shortcodes($content) ) );
return $word_count;
}
add_filter('manage_posts_columns', 'wordcount_column');
function wordcount_column($columns) {
$columns['wordcount'] = 'Word count';
return $columns;
@imelgrat
imelgrat / add-column-middle.php
Last active August 23, 2017 18:11
Add a custom column to Wordpress Post management page after title column: Full article at: http://imelgrat.me/wordpress/customize-wordpress-post-management-page/
add_filter('manage_posts_columns', 'add_column_middle'); // Register filter
function add_column_middle($defaults)
{
$new_columns = array(); // Create empty array to store new column order
foreach ($defaults as $key => $value)
{
$new_columns[$key] = $value; // Add columns to new array
if ($key == 'title')
@imelgrat
imelgrat / products-post-type.php
Last active August 23, 2017 18:06
Declare and configure a new custom post type (Product) in WordPress. Full article at: http://imelgrat.me/wordpress/bulk-upload-custom-posts-wordpress/
<?php
// Only declare the function if it doesn't exist (prevents PHP fatal error)
if (!function_exists('product_post_type'))
{
function product_post_type() // Function to register new custom post type
{
// Labels used inside the WordPress CMS
$labels = array(
'name' => _x('Products', 'Post Type General Name', 'text_domain'),
'singular_name' => _x('Product', 'Post Type Singular Name', 'text_domain'),
@imelgrat
imelgrat / bulk-post-create.php
Last active May 30, 2021 04:07
Create posts in WordPress from data in a MySQL database. Full article at: http://imelgrat.me/wordpress/bulk-upload-custom-posts-wordpress/
<?php
// Create posts in WordPress from data in a MySQL database. Full article at: http://imelgrat.me/wordpress/bulk-upload-custom-posts-wordpress/
//Load WordPress functions and plug-ins. Put correct path for this file. This example assumes you're using it from a sub-folder of WordPress
require_once ('../wp-load.php');
$database['hostname'] = 'SERVER';
$database['username'] = 'USER';
$database['password'] = 'PASSWORD';
$database['database'] = 'DATABASE';
@imelgrat
imelgrat / hentry-remove.php
Last active February 9, 2018 14:50
Easy fix to remove 'hentry' CSS class from the post_class() list in WordPress posts
<?php
/**
* @author Ivan Melgrati
* @copyright 2018
*/
/**
* Easy fix to remove 'hentry' from the post_class() list
*
@imelgrat
imelgrat / check-youtube-id.php
Last active April 27, 2018 05:27
Verify whether a YouTube video exists using PHP and oEmbed status codes. Instead of actually fetching the video’s URL using a tool suchas a cURL, we can speed things up by just checking the response headers by using PHP’s get_headers() function. Full article: https://imelgrat.me/json-xml/oembed-protocol-embed-videos-news/
<?php
/**
* @author Ivan Melgrati
* @copyright 2018
*/
function YouTube_Check_ID($videoID)
{
$is_valid = true;
@imelgrat
imelgrat / jquery-ajax-example.js
Last active February 14, 2019 19:59
jQuery AJAX call example
$.ajax(URL, {
success: function(data) {
// Do something with the returned data
},
error: function() {
//Handle error here
}
});
});
@imelgrat
imelgrat / EmmAppConfig.js
Created February 14, 2019 20:54
AppConfig and Cordova: Getting EMM configuration data with the "Cordova Plugin for EMM App Config"
/**
* AppConfig and Cordova.
*
* Getting EMM configuration data with the "Cordova Plugin for EMM App Config".
* Callback for "deviceready" event.
*
* @link https://imelgrat.me/phonegap/appconfig-cordova-emm/
* @author Iván Melgrati.
*/
function onDeviceReady() {
@imelgrat
imelgrat / build.json
Created February 14, 2019 21:02
XCode 10 build upgrade makes Cordova builds fail. Make it work again
"ios": {
"release": {
"buildFlag": [
"-UseModernBuildSystem=0"
]
},
"debug": {
"buildFlag": [
"-UseModernBuildSystem=0"
]