WordPress functions.php:如何应用update_option()? [英] WordPress functions.php: how to apply update_option()?

查看:55
本文介绍了WordPress functions.php:如何应用update_option()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的WP用户设置默认的图像链接URL,以使其不包含默认的URL链接.我已经做过一些研究,并且知道该功能在wp-admin/options.php中:

I'm trying to set the default Image Link URL for my WP users so that it doesn't include the url link as a default. I've done some research, and I know the function is in the wp-admin/options.php:

update_option('image_default_link_type','file');

我不想将其混入核心文件中,而是希望将其放入functions.php中,但是却永远不知道实现这样的东西的正确方法!到目前为止,这就是我的functions.php中的内容:

Rather than mess with the core files, I'd like to put this into the functions.php, but never know the proper way to implement stuff like this! This is what I have so far in my functions.php:

<?php
    update_option('image_default_link_type','none');
?>

这显然不起作用:它需要正确的设置!在functions.php中实现此功能的正确方法是什么?

This obviously doesn't work: it needs the proper setup! What is the correct way to implement this in functions.php?

另外:我想知道将来自己如何实现这样的功能的策略?例如,我永远不知道是否应该使用add_filter或do_action,以及如何传递参数.我还没有找到一本书或在其中发布任何内容,可以很好地说明这一点,并且可以通过示例向我展示.任何好的线索也将很棒!

Also: I'd like to know the strategy for figuring out how to implement functions like this in the future by myself? For example, I never know whether or not I'm supposed to use add_filter or do_action, and how I need to pass the parameters. I've yet to find a book or post out there that explains this very well, and can show me by example. Any good leads on this would be awesome too!

推荐答案

从Wordpress编解码器开始.请访问插件API (实际上是您正在做的事情),其中介绍了挂钩,操作和过滤器.然后,请参阅操作参考,其中提供了钩子列表.

Start with the Wordpress codex. Visit the plugin API (which is really what you are doing) that explains Hooks, Actions and Filters. Then see the Action Reference which provides your list of hooks.

在这里您会找到钩子 update_option_OPTIONNAME .来自法典的说明:

Here you will find the hook update_option_OPTIONNAME. Description from codex:

由update_option更新WordPress选项后运行功能.动作函数参数:旧选项值,新选项价值.您必须为所需的特定选项添加操作响应,例如update_option_foo在选项"foo"出现时响应已更新.

Runs after a WordPress option has been update by the update_option function. Action function arguments: old option value, new option value. You must add an action for the specific options that you want to respond to, such as update_option_foo to respond when option "foo" has been updated.

从asker的评论中添加代码:

Adding code from asker's comment:

function inventory_linkurl_setting() { 
   update_option('image_default_link_type','none'); 
} 
add_action('admin_init', 'inventory_linkurl_setting'); ?>

这篇关于WordPress functions.php:如何应用update_option()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆