1

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!

Marc P
  • 606
  • 1
  • 11
  • 26

1 Answers1

2

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('admin_init', 'inventory_linkurl_setting'); ?>
mrtsherman
  • 39,342
  • 23
  • 87
  • 111
  • Thanks! This was very helpful. This is what I came up based on your help and that documentation: `` It appears to have worked! Let me know if there's a better way of writing this function. – Marc P Sep 04 '11 at 11:50
  • One problem I noticed with the above function is that it **always** removes the URL. So if I had a value for the Link URL either previously or after I saved it, it always removes the URL. How would I go about applying this function so that the Link URL is set to 'none' only when it's first uploaded, but preserves the URL if it was previously saved or if it's added manually? – Marc P Sep 04 '11 at 12:01
  • See get_option - Just check it before setting it to none. http://codex.wordpress.org/Function_Reference/get_option – mrtsherman Sep 05 '11 at 01:55
  • Thanks, will look into it! (Still having problems, but confident I'll figure it out!) – Marc P Sep 09 '11 at 03:42
  • WARNING: This is a loop and can break your site due to db overload. – Julian Wagner Nov 03 '20 at 16:51
  • 1
    @JulianWagner - thank you for pointing this out. I changed the example code to execute on admin_init rather than on an option update. Cheers! – mrtsherman Dec 14 '20 at 15:31