删除用户元,但仅当在数组中找到时

时间:2021-12-13 作者:Jayce

我创建了一个保存按钮,将页面的URL保存到数组中的用户元。我现在试图创建一个删除按钮,但我发现当使用delete\\u user\\u meta时,它会删除数组中的所有条目,以及与当前页面相等的条目。

我对这一切都很陌生,所以也许这不是最好的方式。。。有没有关于如何删除数组中等于$currentpage的部分的提示?

<?php
$currentpage = home_url( add_query_arg( NULL, NULL ) ); //Get current page
$user_id = get_current_user_id(); //Get current user ID

if(!$user_id){ 
        wp_loginout(); 
} else {
if(isset($_POST["submit"])){
    $data["pluginlink"] = $_POST["pluginlink"];
    foreach ($data as $key => $value) 
        {
        add_user_meta($user_id, $key, $currentpage);
        }//end for each
}//end else 
}//end if user id
if(!$user_id){ 
        wp_loginout(); 
} else {
if(isset($_POST["Remove"])){
    $data["pluginlink"] = $_POST["pluginlink"];
    foreach ($data as $key => $value) 
        {
        delete_user_meta($user_id, $key, $currentpage);
        }//end for each
}//end else 
}//end if user id
$userdata = get_userdata($user_id);
$meta = get_user_meta($user_id);
$data["pluginlink"] = $meta["pluginlink"][0];
?>
<?php 
if(in_array($currentpage,$meta["pluginlink"])) {
    echo "Page has already been saved";
    $savebutton = "display: none;";
    $deletebutton = "display: block;";
    } else {
        echo "page not saved yet";
        $savebutton = "display: block;";
        $deletebutton = "display: none;";
        }
?>
<div class="buttondisplay" style=" <?php echo $savebutton; ?> ">
<form method="post" enctype="multipart/form-data">
    <table class="ui collapsing striped table">
        <tr></tr>
        <tr><td>Current URL: <?php echo $currentpage; ?></td>
         <td><input type="submit" name="submit" value="Save" class="ui blue mini button"></td></tr>
    </table>
</form>
</div><!-- end save button -->

<div class="deletebutton" style=" <?php echo $deletebutton; ?> ">
<form method="post" enctype="multipart/form-data">
    <table class="ui collapsing striped table">
        <tr></tr>
        <tr><td>Current URL: <?php echo $currentpage; ?></td>
         <td><input type="submit" name="Remove" value="Remove" class="ui blue mini button"></td></tr>
    </table>
</form>
</div><!-- end delete button -->

1 个回复
SO网友:Jayce

我想出来了。。。我必须改变

$data["pluginlink"] = $meta["pluginlink"][0];

$data["pluginlink"] = $meta["pluginlink"];

相关推荐

如何从USERS-edit.php中删除颜色选择器代码

我知道我不应该改变Wordpress的核心。但如果我想在“用户编辑”中使用仪表板用户配置文件页面。php和删除大块代码(如颜色选择器)的方法。从第259行到第336行-我想全部删除。<?php if ( ! ( IS_PROFILE_PAGE && ! $user_can_edit ) ) : ?> <tr class="user-rich-editing-wrap"> <th scope="