• Why this code, not working in my wordpress?

    /*-----------------------------------------------------------------------------------*/
    # SHORT URL
    /*-----------------------------------------------------------------------------------*/
    
    /* If the link has already been created, get it from the post meta instead of making a curl request for every page load. */
    function link_pendek() {
        global $post;
        $_link = get_permalink($post->ID);
        $_existing = get_post_meta($post->ID, 'short_url', true);
        if ($_existing !== false && !empty($_existing)) return;
        $_link = curl_get_result('http://api.bit.ly/v3/shorten?login=USERNAME&apiKey=APIKEY&longUrl='.urlencode($_link).'&format=txt');
        update_post_meta($post->ID, 'short_url', $_link);
    }
    
    /* returns a result from url */
    function curl_get_result($url) {
        $ch = curl_init();
        $timeout = 5;
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }
    add_action('publish_page', 'link_pendek');
    add_action('publish_post', 'link_pendek');
  • The topic ‘add_action function not working when publishing post or page’ is closed to new replies.