WordPress自定义插件开发实践-推荐文章配置插件

项目概况

项目组需要在后台能够自己选择文章,配置首页文章推荐。

解决方案

新开发一个首页推荐配置的插件,管理员登录WordPress后台后,能够选择需要推荐的文章,提交保存时,以此给文章增加一个自定义属性的值,比如post_weight。然后根据自定义属性修改首页的查询,展示配置的推荐文章。

具体实现

1、创建插件文件夹

目录:/wp-content/plugins

本人创建的目录名称是:recommend

2、创建recommend.php文件,编写插件代码

说明:本人开发的插件比较简单,功能单一,所以插件代码都写在一个文件里面,如果功能比较复杂,可以自行进行功能拆分。

<?php
/*
Plugin Name: Recommend Plugin
Plugin URI:  https://developer.wordpress.org/plugins/the-basics/
Description: Recommend Plugin Header Comment
Version:     20231127
Author:      Niemx
Author URI:  https://developer.wordpress.org/
Text Domain: wporg
Domain Path: /languages
License:     GPL2

Recommend Plugin is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
any later version.

Recommend Plugin is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with {Plugin Name}. If not, see {License URI}.
*/

function recommend_activate() {

}
register_activation_hook( __FILE__, 'recommend_activate' );

function recommend_deactivate() {
    //--------------Clear the credentials once deactivated-------------------
//    delete_option('zmail_integ_client_id');
}

register_deactivation_hook( __FILE__, 'recommend_deactivate' );

function recommend_integ_settings() {
    add_menu_page (
        'Welcome to Recommend Plugin',
        'Recommend',
        apply_filters( 'recommend_capability', 'read' ),
//        '',
        'recommend-integ-settings',
        'recommend_home_integ_settings_callback' ,
        'dashicons-admin-generic'
    );
}

function recommend_home_integ_settings_callback(){
    global $current_user;
    $bool_can = false;//权限控制
    if( $current_user->roles[0] == 'editor' ||  $current_user->roles[0] == 'administrator') {
        $bool_can = true;
    }
    if( $bool_can ) {
        if(isset($_POST['home_recommend_submit']) && !empty($_POST)){
            //原始数据全部置为0
            $args = array(
                'posts_per_page' => 10,
                'post_type'            => array('post'),
                'post_status'          => array('publish'),
                'ignore_sticky_posts ' => false,
//            'orderby'              => 'date',
                'order'                => 'DESC',
                'meta_key' => 'post_weight',
                'orderby' => 'meta_value_num',

            );
            $post_pre_list = new WP_Query($args);
            while ($post_pre_list->have_posts()) : $post_pre_list->the_post();
                update_metadata( 'post',get_the_ID(), 'post_weight', 0 );
            endwhile;
            for($num = 1,$weight=100;$num<=10;$num++,$weight--){
                if($_POST['recommend_key_'.$num] > 0){
                    update_metadata( 'post',$_POST['recommend_key_'.$num], 'post_weight', $weight );
                }
            }
            echo '<div class="updated"><p><strong>'.esc_html__('Save Successfully').'</strong></p></div>'."\n";

        }
    }

    $args_new = array(
        'posts_per_page' => 10,
        'post_type'            => array('post'),
        'post_status'          => array('publish'),
        'ignore_sticky_posts ' => false,
        'order'                => 'DESC',
        'meta_key' => 'post_weight',
        'orderby' => 'meta_value_num',

    );
    $post_list = new WP_Query($args_new);
?>
    <head>
        <meta charset="UTF-8">
        <title>Recommended configuration for homepage articles</title>
    </head>
    <body>
    <h1 style="margin-top:20px;margin-left: 20px;">Recommended configuration for homepage articles</h1>
    <form method="post" action="<?php echo $_SERVER["REQUEST_URI"]; ?>">
        <?php add_thickbox(); ?>
        <div style="margin-top:20px;margin-left: 20px;">
            <?php $key=1; ?>
            <?php while ($post_list->have_posts()) : $post_list->the_post(); ?>
            <table>
            <?php
                $post_weight = get_metadata( 'post', get_the_ID(), 'post_weight', true );
                if($post_weight>0){ ?>
                <tr>
                    <td width="180px;">Recommended articles(<?php echo $key;?>)</td>
                    <td>
                        <input type="text" value="<?php the_title(); ?>" name="recommend_<?php echo $key;?>" id="recommend_<?php echo $key;?>" style="width: 800px">
                        <?php
                        if( $bool_can ) {
                        ?>
                        <a class="thickbox" title="Article List" href="/wp-admin/post-list.php?TB_iframe=true&width=1000&height=680&table_id=<?php echo $key;?>"><button class="btn_select">Select</button></a>
                        <?php } ?>
                        <input type="hidden" value="<?php the_ID(); ?>" name="recommend_key_<?php echo $key;?>" id="recommend_key_<?php echo $key;?>">
                    </td>
                </tr>
            <?php }else{?>
                <tr>
                    <td width="180px;">
                        Recommended articles(<?php echo $key;?>)
                    </td>
                    <td>
                        <input type="text" value="" name="recommend_<?php echo $key;?>" id="recommend_<?php echo $key;?>" style="width: 800px">
                        <?php
                        if( $bool_can ) {
                        ?>
                        <a class="thickbox" title="Article List" href="/wp-admin/post-list.php?TB_iframe=true&width=1000&height=680&table_id=<?php echo $key;?>"><button class="btn_select">Select</button></a>
                        <?php } ?>
                        <input type="hidden" value="" name="recommend_key_<?php echo $key;?>" id="recommend_key_<?php echo $key;?>">
                    </td>
                </tr>
            <?php }
                $key++;
            endwhile;
            wp_reset_postdata();
        ?>
            </table>
        </div>
        <?php
        if( $bool_can ) {
        ?>
        <div class="form__row form__row-btn" style="margin-left: 20px;"> <input type="submit" class = "btn" name="home_recommend_submit" id="home_recommend_submit" value="<?php _e('Save');?>">
        <?php } ?>
        </div>
    </form>
    <script type="text/javascript">
        function parent_check(_input_id,_value,_title){
            var _input = document.getElementById("recommend_"+_input_id);
            var _input_hidden = document.getElementById("recommend_key_"+_input_id);
            _input_hidden.setAttribute("value", _value);
            _input.setAttribute("value", _title);
            tb_remove();
        }
    </script>
    <style>
        .btn_select {
            display: inline-block;
            padding: 5px 10px;
            background-color: #4c84ff;
            color: #fff;
            font-size: 15px;
            border-radius: 3px;
            border:0px;
            cursor: pointer;
            transition: all 0.3s ease
        }
    </style>
    </body>

<?php
}


add_action('admin_menu','recommend_integ_settings');

插件上传之后,需要登录WordPress后台启用插件。

3、配置页面效果

4、修改首页查询

查询代码

$arg = array(
    'post_type'   =>  'post',
    'post_status' => 'publish',
    'order' => 'DESC',
    'posts_per_page' => 10,
    'meta_key' => 'post_weight',
    'orderby' => 'meta_value_num',
);
$queryd = new \WP_Query( $arg );

使用查询的结果集

while ($queryd->have_posts()) : $queryd->the_post();

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
开发WordPress支付插件的步骤如下: 1. 创建插件文件夹:在WordPress插件目录下创建一个新的文件夹,用于存放你的支付插件代码。 2. 创建主插件文件:在插件文件夹中创建一个主插件文件,命名为`your-plugin-name.php`,并在文件中添加必要的插件信息和初始化代码。 3. 添加钩子函数:使用WordPress的钩子函数来注册插件的激活和停用事件,以及其他需要的钩子函数。 4. 创建支付页面:创建一个用于接收支付请求的页面模板,可以使用WordPress的页面模板功能来创建一个自定义的页面模板。 5. 处理支付请求:在主插件文件中添加处理支付请求的代码,包括验证支付参数、生成订单、调用支付接口等。 6. 添加设置页面:如果你的支付插件需要用户配置参数,可以创建一个设置页面,让用户输入必要的配置信息。 7. 集成支付接口:根据你选择的支付接口,按照接口提供的文档和示例代码,将支付接口集成到你的插件中。 8. 测试和调试:在开发过程中,使用WordPress的调试工具和日志功能来测试和调试你的支付插件。 9. 发布和部署:完成开发和测试后,将你的支付插件打包成zip文件,并上传到WordPress插件管理页面进行安装和启用。 10. 提供文档和支持:为你的支付插件编写详细的文档,包括安装和配置说明,以及常见问题解答。同时,提供技术支持,帮助用户解决使用过程中遇到的问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值