Skip to content

Instantly share code, notes, and snippets.

@woodwardtw
Last active October 17, 2015 03:45
Show Gist options
  • Save woodwardtw/81433e2d12596ebf4e75 to your computer and use it in GitHub Desktop.
Save woodwardtw/81433e2d12596ebf4e75 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: URL counter
Plugin URI: http://URI_Of_Page_Describing_Plugin_and_Updates
Description: This describes my plugin in a short sentence
Version: 1.5
Author: Tom Woodward
Author URI: http://bionicteaching.com
License: GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Domain Path: /languages
Text Domain: my-toolset
*/defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
//the actions that trigger the function
add_action('save_post', 'countURLs');
add_action('draft_to_publish', 'countURLs');
add_action('pending_to_publish', 'countURLs');
function countURLs ($post_id){
$new_post = get_post($post_id);
//gets the post text
$content = $new_post->post_content;
//the regex
$pattern = '"\"https?:\/\/([^\"]*)\""';
//apply the regex to the post put the matches in the $matches array
preg_match_all($pattern, $content, $matches, PREG_SET_ORDER);
//count the number of URLs in the array
$count = (count($matches));
//write the count of URLs to the URL_count custom field
update_post_meta($new_post->ID, 'URL_count', $count);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment