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