dongtaotao19830418 2016-08-01 04:55
浏览 62
已采纳

从表单传递id到删除功能

I have created the following wordpress page template:

<?php
/* Template Name: Report Creator */ 

get_header(); 
wp_enqueue_style( 'wp-admin' );

?>

    <div id="primary" class="content-area">
        <main id="main" class="site-main" role="main">

            <?php
?>
    <style>
        .table td{
            padding-left:0;
        }
    </style>

<!-- ############################### -->
<?php
 if(isset($_POST['function-action'])){
        $function_action = $_POST['function-action'];
        switch($function_action){
            case 'form-edit-profile':


                break;

            case 'form-delete-profile':

                wp_delete_post(); //pass ID over here to delete the post
                break;
            default:
                break;
        }
    }
?>

<table> 
<tr>
<th>Titel</th>
<th>Creation Date</th>
<th>Next fetch time</th>
<th># of Recipience</th>
<th>Functions</th>
</tr>
<?php
if ( is_user_logged_in() ):

    global $current_user;
    $author_query = array('post_status' => array( 'draft' ), 
    'author' => $current_user->ID,
    'meta_query'             => array(
        array(
            'key'       => 'wpgamail_options',
            'key'       => 'ganalytics_settings',
        ),
    ),);
    $author_posts = new WP_Query($author_query);
    while($author_posts->have_posts()) : $author_posts->the_post();
    ?>
    <tr>
    <td>
        <?php the_title(); ?> 
    </td>
    <td>
        <?php $post_date = get_the_date(); echo $post_date; ?>
    </td>
    <td>
        <?php $field = get_field('wpgamail_options', get_the_ID(), true); echo "Next report on " . date('l jS \of F Y h:i:s A', $field['nextfetchtime']); ?>
    </td>
    <td>
        <?php echo count($field['recipients']) . " Recipient"; ?>
    </td>
    <td>
        <form method="post">
        <input type="hidden" name="function-action" value="form-edit-profile"/>
            <input type="submit" value="Edit Profile" class="button button-primary" />
        </form>
    </td>
    <td>
        <form method="post">
        <input type="hidden" name="function-action" value="form-delete-profile"/>
            <input type="submit" value="Delete Profile" class="button button-primary" onclick="return confirm('Do you really want to delete the report-settings?')" />
        </form>
    </td>
    </tr>    
    <?php           
    endwhile;

else :
    echo "You are not logged in. Please log in!";
endif;
?>
</table>
<hr/>

        </main>
        <!-- .site-main -->

    </div>
    <!-- .content-area -->
<?php get_footer(); ?>

My problem is that I do not know how to pass the id of the currently selected post to the switch-caseconstruct in order to delete / edit the post.

Any suggestions how to do that?

I appreciate your replies!

  • 写回答

1条回答 默认 最新

  • douzhuo8312 2016-08-01 05:18
    关注

    Try this.

    Your HTML

    <form method="post">
        <input type="hidden" name="function-action" value="form-delete-profile" />
        <input type="hidden" name="this_id" value="<?php echo get_the_ID(); ?>" />
        <input type="submit" value="Delete Profile" class="button button-primary" onclick="return confirm('Do you really want to delete the report-settings?')" />
    </form>
    

    Your PHP

    <?php
     if(isset($_POST['function-action'])){
            $function_action = $_POST['function-action'];
            $this_id         = (int)$_POST['this_id'];
            $post_author     = get_post_field( 'post_author', $this_id ); //Get post's author
            switch($function_action){
                case 'form-edit-profile':
    
    
                    break;
    
                case 'form-delete-profile':
                    if( $post_author == get_current_user_id() ) { //This condition is to check that currect is the author of this post
                        wp_delete_post( $this_id ); //pass ID over here to delete the post
                    } else{
                        echo "You don't have permission to delete it.!";
                    }
                    break;
                default:
                    break;
            }
        }
    ?>
    

    Good luck

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程