繁体   English   中英

从Functions.php访问插件数据

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM