WordPress functions.php: how to apply update_option()?

偶尔善良 提交于 2020-01-15 18:50:55

问题


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');

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');
?>

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

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!


回答1:


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.

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

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.

Adding code from asker's comment:

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


来源:https://stackoverflow.com/questions/7297400/wordpress-functions-php-how-to-apply-update-option

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!