如何链接到有快捷代码的页面?

时间:2014-11-09 作者:David Labbe

我正在构建一个日历插件,可以通过短代码添加到页面中。我试图找到如何建立一个链接,链接到日历的短代码所在的页面。换句话说,如果用户将[日历快捷码]添加到页面,我希望能够从站点内的任何位置链接到该页面。到目前为止,这是我想到的,但我想知道是否还有其他方法。

/**
* Get page shortcode is on.
*
* @param $shortcode string. The shortcode tag
* @param $transient string. Name of transient
* @return array|mixed
*/
function get_shortcode_page($shortcode, $transient){

if(false === ($shortcode_page = get_transient($transient))){

    //Search query for shortcode tag
    $the_query = new WP_Query(array(\'s\' => $shortcode));

    //Build simple array of page info to pass into transient
    if(isset($the_query) && $the_query->post_count != 0){
        $shortcode_page = array(\'ID\' => $the_query->posts[0]->ID, \'permalink\' => get_permalink($the_query->posts[0]->ID));

        set_transient($transient, $shortcode_page);
    }
    return null;

   }
  return $calendar_page;
}

//Use function to find shortcode page info
$page = get_shortcode_page(\'class-schedule\', \'shortcode_calendar_page\');

 /**
 * Delete shortcode transients
 *
 * If page permalink changes then delete old transients.
 *
 *
 * @param $post_id
 */
function delete_calendar_shortcode($post_id){

//get shortcode page.
$calendar_page = get_shortcode_page(\'class-schedule\', \'shortcode_calendar_page\');

    if($post_id != $calendar_page[\'ID\'])
        return $post_id;

    $current_permalink = get_permalink( $post_id );

    if ($current_permalink != $calendar_page[\'permalink\']){
        delete_transient( \'shortcode_calendar_page\' );
   }

   return $post_id;
}

add_action(\'save_post\',  \'delete_calendar_shortcode\');
这还没有经过太多的测试,但确实有效。我想知道是否有更有效的方法?

谢谢

更新时间:

下面是上面的版本2:这将消除对上面的get\\u shortcode\\u page()函数的需要。一切都将在保存页面上处理。

/**
 * Delete shortcode transients vs 2
 *
 * If page permalink changes then delete old transients.
 *
 *
 * @param $post_id
 */
function delete_calendar_shortcode_vs2($post_id){

// If this is an autosave, our form has not been submitted,
if(defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE){
    return $post_id;
}

$content_post = get_post($post_id);
$content = $content_post->post_content;

//only run if current page being saved has the shortcode
if(has_shortcode($content, \'class-schedule\')){

    //Build simple array of page info to pass into transient
    $page = array(\'ID\' => $post_id, \'permalink\' => get_permalink($post_id));

    if(false !== ($calendar_page = get_transient(\'shortcode_calendar_page\'))){

        if($page[\'ID\'] != $calendar_page[\'ID\'] || $page[\'permalink\'] !=   $calendar_page[\'permalink\']){
            delete_transient(\'shortcode_calendar_page\');
        }
    }

    set_transient(\'shortcode_calendar_page\', $page);
  }

return $post_id;
}

 add_action(\'save_post\', \'delete_calendar_shortcode_vs2\');

//get shortcode page from transient
$page = get_transient(\'shortcode_calendar_page\');
再次。。。我想看看是否还有其他更有意义的方法。我还需要根据短代码所在的页面添加重写规则。我想一步一步来。谢谢

////////借助@true更新V3////////////

    /**
     * Shortcode page meta builder
     *
     * Checks on save if shortcode for page exist and saves info to meta.
     *
     * @param $post_id
     */


     function has_shortcode_meta_builder($post_id){

        // If this is an autosave, our form has not been submitted,
        if(defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE){
            return $post_id;
        }

        $content_post = get_post($post_id);
        $content = $content_post->post_content;

        if(has_shortcode($content, \'class-schedule\')){
            update_post_meta($post_id, \'has_shortcode\', \'1\');
            delete_transient(\'shortcode_pages\');

        }else{
            //check if meta exist in the event shortcode was removed from page.
            $meta = get_post_meta($post_id, \'has_shortcode\');
            if(!empty($meta)){
                delete_post_meta($post_id, \'has_shortcode\');
                delete_transient(\'shortcode_pages\');
            }
        }

        return $post_id;
    }

    add_action(\'save_post\', \'has_shortcode_meta_builder\');
然后,此函数将用于查找所有带有短代码的页面。返回瞬态结果或对新结果进行sql调用以重建瞬态。

    /**
     * Get all posts and their permalinks that have shortcode.
     * Find pages with shortcode and store them into transient
     *
     * @global wpdb $wpdb
     * @return array. 
     */
    public function get_pages_with_shortocde(){
        global $wpdb;

        if(false === ($post_data = get_transient(\'shortcode_pages\'))){

            $post_data = $wpdb->get_results($wpdb->prepare(
                "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s", \'has_shortcode\'));

            if(!empty($post_data)){
                foreach($post_data as $row){
                    $post = get_post($row->post_id);
                    $row->slug = $post->post_name;
                    $row->post_permalink = get_permalink($row->post_id);
                }
            }
            set_transient(\'shortcode_pages\', $post_data);
        }

        return $post_data;
    }
最后添加一个操作,这样当帖子被删除时,它会检查页面中是否有短代码。如果确实如此,则删除短码瞬态(下次调用时将刷新)

    /**
     * Delete actions when post is deleted.
     *
     * @param $post_id
     */
     function delete_post_data($post_id){
        global $wpdb;

        //Delete shortcode transient so that it can be rebuilt excluding this page
        $content_post = get_post($post_id);
        $content = $content_post->post_content;
        if(has_shortcode($content, \'class-calendar)){
            delete_transient(\'shortcode_pages\');
        }

    }
 add_action(\'before_delete_post\', \'delete_post_data\');

1 个回复
SO网友:true

瞬变可能不是解决问题的方法。

据我所知:

您想通过某种形式的查找来了解帖子是否有短代码

您希望能够获得具有此短代码的永久链接/帖子列表。

这是我的建议。

/**
 * Checks for calender shortcode and flags the post with post meta.
 * @param integer $post_id
 * @return integer
 */
function save_calender_postmeta($post_id)
{
    // Make sure this isn\'t an autosave.
    if(!defined(\'DOING_AUTOSAVE\') || !DOING_AUTOSAVE){
        // Get Post Content
        $content = get_post_field(\'post_content\', $post_id);
        // Check for shortcode
        if(has_shortcode($content, \'class-schedule\')) {
            // Update (or it will insert) the \'has_calender\' post meta.
            update_post_meta($post_id, \'has_calender\', \'1\');
        }
        else {
            // Delete the has calender post meta if it exists.
            delete_post_meta($post_id, \'has_calender\');
        }
    }
    return $post_id;
}
add_action(\'save_post\', \'save_calender_postmeta\');
然后,如果你想得到所有有日历的帖子,你可以使用:

/**
 * Get all posts and their permalinks that have calenders.
 * @global wpdb $wpdb
 */
function get_calender_posts_links()
{
    global $wpdb;
    $rows = $wpdb->get_results($wpdb->prepare(
            "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s",
            \'has_calender\'
    ));
    if(!empty($rows)) {
        // Let\'s include the post permalinks with each row.
        foreach($rows as $row) {
            $row->post_permalink = get_permalink($row->post_id);
        }
    }
    return $rows;
}
或者,如果您想检查一篇文章是否有日历:

/**
 * Check if a post has a calender.
 * @param int $post_id
 * @return boolean
 */
function post_has_calender($post_id)
{
    return (get_post_meta($post_id, \'has_calender\'));
}
如果这是你要找的,请告诉我。

结束

相关推荐

OOP and WordPress shortcode

我试图通过这种方式添加一个短代码class MyPlugin { function __construct() { $this->make_shortcode(); } function ShowMsg($cls, $lst4) { $data = shortcode_atts(array(\'phn\' => \'\', \'msg\' => \'\'), $atts);