[英]Access Plugin data from Functions.php
我正在嘗試將來自wordpress帖子的數據插入到新的表數據庫表中,而不是WP_postmeta
。 我的函數正在運行,它將完全插入$postid
。 我遇到的問題是我需要從Marty Spellerbergs“地址地理編碼器”插件中插入緯度和經度坐標。
在可以在https://wordpress.org/plugins/address-geocoder/此處看到的插件頁面上,Marty說,您可以使用以下命令訪問循環內的cooridnate:
<?php echo get_geocode_lng( $post->ID ); ?>
<?php echo get_geocode_lat( $post->ID ); ?>
現在我知道位於functions.php文件中,實際上我們不在lood中,因此我嘗試了許多不同的方法來訪問此數據,但我做不到。 有沒有辦法編輯Marty指定的行,以便可以在函數中調用它們?
這是我為此所做的許多嘗試之一:
function save_lat_lng( $post_id )
{
global $wpdb;
global $post;
$custom_lat = $_POST[get_geocode_lat( $post->ID )];
$custom_lng = $_POST[get_geocode_lng( $post->ID )];
// Check that we are editing the right post type
if ( 'festival-event' != $_POST['post_type'] )
{
return;
}
// Check if we have a lat/lng stored for this property already
$check_link = $wpdb->get_row("SELECT * FROM lat_lng_post WHERE post_id = '" . $post_id . "'");
if ($check_link != null)
{
// We already have a lat lng for this post. Update row
$wpdb->update(
'lat_lng_post',
array(
"lat" => $custom_lat,
"lng" => $custom_lng
),
array( 'post_id' => $post_id ),
array(
'%f',
'%f'
)
);
}
else
{
// We do not already have a lat lng for this post. Insert row
$wpdb->insert(
'lat_lng_post',
array(
'post_id' => $post_id,
"lat" => $custom_lat,
"lng" => $custom_lng
),
array(
'%d',
'%f',
'%f'
)
);
}
}
add_action( 'save_post', 'save_lat_lng' )
如果您將post_id
作為參數傳遞給函數save_lat_lng,我認為您應該更改這兩行:
$custom_lat = $_POST[get_geocode_lat( $post->ID )];
$custom_lng = $_POST[get_geocode_lng( $post->ID )];
至
$custom_lat = get_geocode_lat( $post_id );
$custom_lng = get_geocode_lng( $post_id );
訪問該參數。
另外,如果您要檢查帖子類型,則應更改
if ( 'festival-event' != $_POST['post_type'] )
至
if ( 'festival-event' != get_post_type($post_id) )
以下文檔:
https://codex.wordpress.org/Function_Reference/get_post_type
PS。 下次您應該粘貼插件鏈接時,可以節省時間:)
-更新
坐標不可用-問題與save_post掛鈎優先級值有關。
從插件代碼中,我看到更新post meta的功能具有該優先級:
add_action( 'save_post', array( $this, 'save_post' ), 10, 2 );
因此,要在那之后運行鈎子,應該設置以下內容:
add_action( 'save_post', 'save_lat_lng', 100 );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.