2

My site has image attachment pages disabled through Yoast SEO, but some of our content editors frequently insert images with the option of Link To: Attachment Page.

If I speak to them individually, I have them insert an image and specifically choose Link To: None, and from then on their default is Link To None, as desired. This means that whenever they insert an image, it's just an image.

Clearly, WP stores this default somewhere, but I haven't been able to find it.

I've used /wp-admin/options.php to change image_default_link_type to none, but it didn't change for the users who were defaulting to Attachment Page. They still do, unless I manually select None and insert an image using their profile.

Hoping there's a MySQL query or some function override so that when any user on my site adds images, they'll default to Link To None. I don't want to change the output - I want to actually remove the <a></a> tags that are wrapped around images in the editor when anyone adds an image.

6
  • Have you tried the method outlined in this answer?
    – DaveLak
    Commented Jun 9, 2017 at 19:42
  • No - I'll save it as a backup, but as I mentioned I'm trying to prevent the links from being created in the first place, rather than filtering them out later.
    – WebElaine
    Commented Jun 9, 2017 at 19:47
  • 1
    How about this one wordpress.stackexchange.com/questions/87357/… Commented Jun 9, 2017 at 20:35
  • 1
    That works. Thank you! It still seems strange that WP can "remember" something like this but it's not exposed somewhere where it can be manipulated.
    – WebElaine
    Commented Jun 9, 2017 at 21:05
  • 2
    It's stored in user meta under the key $prefix . 'user-settings', where $prefix is your table prefix set in wp-config. The settings are all in a sort of querystring, that specific item is urlbutton. I'm not sure how you're supposed to update it, I know it also gets put into cookie.
    – Milo
    Commented Jun 9, 2017 at 21:12

1 Answer 1

2

This is the final non-JS method that finally worked for me:

Step 1: update sitewide option

You can do this several ways. Since setting it is a one-time operation, I opted to update this by visiting https://www.example.com/wp-admin/options.php . I searched for "image_default_link_type" and set it to "none" (the word none, not null), then saved the settings.

Unfortunately, saved usermeta overrides this sitewide setting - so the problem links were continuing to appear at this point.

Step 2: update all users

Many thanks to @Milo for revealing the location this is stored under - usermeta, buried in a query string of options that may appear in any order.

In theory this could be a one-time operation, or if you think users may continually update their defaults, you can set this to run on a hook like wp_dashboard_setup so every time someone views the dashboard, all users' "urlbutton" options are removed, which causes the site to use the sitewide "image_default_link_type". I opted to go with run-on-dashboard-view.

add_action('wp_dashboard_setup', 'wpse_update_user_defaults');
function wpse_update_user_defaults() {
    $users = get_users();
    foreach($users as $user) {
        // pull current settings
        $wpUserSettings = get_user_meta($user->ID, 'wp_user-settings', true);   
        // if 'wp_user-settings' is present with a 'urlbutton' value
        if(!empty($wpUserSettings) && strpos($wpUserSettings, 'urlbutton') !== false) {
            // if 'urlbutton' is not the only setting
            if(strpos((string)$wpUserSettings, '&') !== false) {
                // if there's an & before (may or may not have an & after)
                $firstUpdate = preg_replace("/&urlbutton=[^&]*/", '', $wpUserSettings);
                // if there's an & after (but not before)
                $secondUpdate = preg_replace("/^urlbutton=[^&]*&/", '', $firstUpdate);
                // save updated meta
                update_user_meta($user->ID, 'wp_user-settings', $secondUpdate);
            // if 'urlbutton' is the only setting
            } else {
                delete_user_meta($user->ID, 'wp_user-settings');
            }
        }
    }
}

After I got this sorted out, I searched for existing posts with the problem by using this query:

SELECT * FROM wp_posts WHERE post_content LIKE "%rel=\"attachment%"

and from there edited all the posts manually to remove the tags.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.