[英]Access Plugin data from Functions.php
I'm trying to insert data from a wordpress post into a new table database table rather than WP_postmeta
. 我正在尝试将来自wordpress帖子的数据插入到新的表数据库表中,而不是WP_postmeta
。 My function is working, It'll insert the $postid
no problem at all. 我的函数正在运行,它将完全插入$postid
。 The issues I'm having is I need to inser the latitude and longitude coordinates from Marty Spellerbergs 'Address Geocoder' Plugin. 我遇到的问题是我需要从Marty Spellerbergs“地址地理编码器”插件中插入纬度和经度坐标。
On the plugin page which can be seen here https://wordpress.org/plugins/address-geocoder/ , Marty says you can access the cooridnates inside the loop using these: 在可以在https://wordpress.org/plugins/address-geocoder/此处看到的插件页面上,Marty说,您可以使用以下命令访问循环内的cooridnate:
<?php echo get_geocode_lng( $post->ID ); ?>
<?php echo get_geocode_lat( $post->ID ); ?>
Now I know being inside the functions.php file, we are not actually inside the lood, so I've tried lots of different ways of accessing this data but I just can't do it. 现在我知道位于functions.php文件中,实际上我们不在lood中,因此我尝试了许多不同的方法来访问此数据,但我做不到。 Is there a way to edit those lines specified by Marty so they can be called in the functions?. 有没有办法编辑Marty指定的行,以便可以在函数中调用它们?
This is one of my many many attempts at this: 这是我为此所做的许多尝试之一:
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' )
if you are passing post_id
as parameter to function save_lat_lng i think that you should change that two lines: 如果您将post_id
作为参数传递给函数save_lat_lng,我认为您应该更改这两行:
$custom_lat = $_POST[get_geocode_lat( $post->ID )];
$custom_lng = $_POST[get_geocode_lng( $post->ID )];
to 至
$custom_lat = get_geocode_lat( $post_id );
$custom_lng = get_geocode_lng( $post_id );
to access that parameters. 访问该参数。
also if you want to check post type you should change 另外,如果您要检查帖子类型,则应更改
if ( 'festival-event' != $_POST['post_type'] )
to 至
if ( 'festival-event' != get_post_type($post_id) )
docs below: 以下文档:
https://codex.wordpress.org/Function_Reference/get_post_type https://codex.wordpress.org/Function_Reference/get_post_type
PS. PS。 next time you should paste link for plugin, it saves time :) 下次您应该粘贴插件链接时,可以节省时间:)
--- UPDATE -更新
Coordinates were not available - problem was related to save_post hook priority value. 坐标不可用-问题与save_post挂钩优先级值有关。
From plugin code i see that function updating post meta have that priority: 从插件代码中,我看到更新post meta的功能具有该优先级:
add_action( 'save_post', array( $this, 'save_post' ), 10, 2 );
so to run your hook after that, you should set something like that: 因此,要在那之后运行钩子,应该设置以下内容:
add_action( 'save_post', 'save_lat_lng', 100 );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.