Last active
October 17, 2015 03:45
-
-
Save woodwardtw/81433e2d12596ebf4e75 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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