WordPress 添加文章自定义字段
像素鱼丸
11-22
111
0

有时候需要给文章添加一些自定义字段,比如、价格和尺寸等自定义字段,参考如下代码。

/**
 * 文章编辑页-添加自定义字段
 */
add_action('add_meta_boxes', 'flame_add_custom_box');
function flame_add_custom_box()
{
    //需要添加自定义字段的文章类型 array('post','page','audio');
    $screens = array('audios');
    foreach ($screens as $screen) {
        add_meta_box(
            'html_meta_div_id',
            __('主题自定义字段', 'field_textdomain'),
            'flame_inner_custom_box',
            $screen
        );
    }
}


/**
 * 显示自定义字段编辑框
 */
function flame_inner_custom_box($post)
{
    // 使用随机数进行核查
    wp_nonce_field(plugin_basename(__FILE__), 'myplugin_noncename');


    // 使用 get_post_meta 从数据库中检索现有的值,并应用到表单中
    $oss_url = get_post_meta($post->ID, 'oss_url', true);
    $rt_author = get_post_meta($post->ID, 'rt_author', true);
    $price = get_post_meta($post->ID, 'price', true);


    echo '
        <div class="rt-post-row">
            <label for="rt_author">音频作者:</label>
            <input type="text" class="form-control" name="rt_author" value="' . esc_attr($rt_author) . '">
        </div>
        <div class="rt-post-row">
            <label for="price">价格:</label>
            <input type="text" class="form-control" name="price" value="' . esc_attr($price) . '" placeholder="单位是分">
        </div>
        ';
}


/**
 * 文章保存时,保存自定义数据
 */
add_action('save_post', 'flame_save_postdata');
function flame_save_postdata($post_id)
{
    // 首先,我们需要检查当前用户是否被授权做这个动作。 
    if ('page' == $_POST['post_type']) {
        if (!current_user_can('edit_page', $post_id)) return;
    } else {
        if (!current_user_can('edit_post', $post_id)) return;
    }


    // 其次,我们需要检查,是否用户想改变这个值。
    if (!isset($_POST['myplugin_noncename']) 
        || !wp_verify_nonce($_POST['myplugin_noncename'], plugin_basename(__FILE__)))
        return;


    // 第三,我们可以保存值到数据库中
    //如果保存在自定义的表,获取文章ID
    $post_ID = $_POST['post_ID'];


    // 作者
    $rt_author = sanitize_text_field($_POST['rt_author']);
    add_post_meta($post_ID, 'rt_author', $rt_author, true) or
        update_post_meta($post_ID, 'rt_author', $rt_author);
        
    // 价格
    $price = intval(sanitize_text_field($_POST['price']) );
    add_post_meta($post_ID, 'price', $price, true) or
        update_post_meta($post_ID, 'price', $price);
}

wordpress 自定义分类法 删除文章字段

在WordPress中,如果你想要从自定义分类法(taxonomy)中删除某个文章字段,你可以使用wp_delete_post_meta()函数来删除。以下是一个简单的例子,展示了如何在删除文章时同时删除与其相关联的自定义分类法字段。

// 钩子函数,在删除文章时触发
add_action( 'delete_post', function( $post_id ) {
    // 删除文章的自定义字段,例如 'your_taxonomy_field_key'
    delete_post_meta( $post_id, 'your_taxonomy_field_key' );
 
    // 如果你想要删除关联的分类法信息,可以这样做
    $terms = wp_get_object_terms( $post_id, 'your_taxonomy' );
    foreach ( $terms as $term ) {
        wp_remove_object_terms( $post_id, $term->term_id, 'your_taxonomy' );
    }
});

在这个例子中,your_taxonomy_field_key是你想要删除的文章字段的键,your_taxonomy是你的自定义分类法名称。这段代码将在每次删除文章时运行,并清除与该文章关联的自定义字段和分类法信息。

收藏
打赏
WordPress 的用户角色和权限
上一篇
Wordpress 增加文章阅读次数
下一篇

发表评论

像素鱼丸
147 文章
0 评论
4 喜欢
最新文章

详解 WordPress 的评论设置

好的,我们来详细梳理并总结 WordPress 中关于文章评论的两个核心控制层级:全局设置和单篇设置。理解这两者的关系(优先级)是管理网站评论的关键。 1. 全局设置 (Global Settings) —— 网站的“默认规则” 这是整个网站评论系统的总开关和默认行为准则。它决定了新发布的文章默认是什么样子的。 位置:WordPress 后台仪表盘 -> 设置 (Settings) -> […]

wp_handle_comment_submission 函数

wp_handle_comment_submission() 是 WordPress 中用于处理评论提交的核心函数之一。它通常在用户提交评论时被调用,负责验证和处理评论数据,并最终将评论插入到数据库中。 ✅ 函数作用 wp_handle_comment_submission() 的主要功能是: 验证用户提交的评论数据(如评论内容、用户名、邮箱等) 检查是否为垃圾评论(通过 Akismet 或其他过 […]

Mirage 主题 v2.89.0 发布

Mirage 主题 v2.89.0 发布 feat 懒加载图片增加灰色背景颜色 refactor 移除一些无用的js文件 fix 优化卡片列表样式 feat 如果设置里删除logo,就直接显示网站名称 fix 修复pc下拉菜单宽度的一个样式bug fix 修复未登录用户取消喜欢时产生的一个错误 fix 优化 wp_vt_star 表结构 下载地址:https://gitee.com/vthemec […]

WordPress set_transient()使用方法和实现

set_transient() 是 WordPress 中用于设置临时数据的函数,它允许你将数据存储在缓存中,这些数据会在指定的时间后自动过期。它是 WordPress 提供的 transient API 的一部分,常用于缓存数据库查询、API 响应或其他计算密集型操作的结果。 一、基本概念 1. 什么是 Transient? Transient 是 WordPress 中的一种缓存机制,类似于 […]
生成中...
扫描二维码
扫描二维码
用户登录