Skip To Content

How to update ACF options using ManageWP’s Code Snippets

Marek Grabowiecki

Director of Client Experience

Date

Jun. 27, 2018

If you work for a Web Development or Digital Marketing Agency then you are probably using a tool like ManageWP to monitor and maintain your clients’ websites. There are tons of benefits to using these types of tools which includes being able to update plugins, manage admin access, and run php code snippets against your sites straight from the tool’s Dashboard.

We recently found out about ManageWP’s ability to run php code snippets against our managed sites and it got us excited. After a bit of testing, we found out that we could run the same snippet against multiple websites at the same time and this got us even more excited! The reason why we were so excited is because we could now update our ACF options using ManageWP’s Code Snippets feature to edit content across multiple websites by running 1 command, which would save us tons of time.

When Would I Use The Code Snippets Feature?

This is something that you would use when you need to make a small update on a website, but also need it to occur across a large number of projects.

many-projects-need-to-be-updated

A scenario recently came up for our team when our company’s name switched from nvision solutions to nvision. Since the credits that appear on our existing sites would now be using the wrong company name, we would have to go through and make this same update on each project.

Why Would I Use The Code Snippets Feature?

Taking the above scenario into account; If we had to go through each one of our clients’ sites manually to update our credits, it would be a fairly time consuming process. Luckily, we are big believers in using ACF to control things like this and have chosen to store these elements in the database instead of hard coding them into our themes.

Using a simple script in ManageWP’s Code Snippets feature, we are able to run a query against the database to update our ACF fields instead of having to go into the backend of each website to change these values manually.

This is where the majority of the time savings come in! We were able to update our credits link across hundreds of website within minutes. If we had to go through each website and update the link manually, we’d be looking at hours if not days of work to complete all of the updates.

How Do I Use The Code Snippets Feature?

After you’ve logged in to ManageWP, head on over to your managed websites. Select a website (or multiple websites) and click the Tools button. From there you’ll be presented with a number of options. Choose the Code Snippets option:

 

Once you’ve arrived at the Code Snippets page, you’ll notice that you’re presented with a field to provide some php code. This is where you’ll want to add your code and logic that will be responsible for updating your ACF field values.

managewp-php

After you’ve added your code to the editor, there are 2 actions that you can continue with. Save and Run.

If you save the snippet it will be available for use at a later time. This is a handy feature to have if you ever see yourself needing to run the same code snippet in the future.

To reuse a Code Snippet simply click on one of the saved Code Snippets and press the Load button.

managewp-saved-snippets

If you choose to run a code snippet, the code that is in the editor will be executed against the live website.

You’ll receive a response back from the website letting you know whether an error occurred or whether the code executed successfully. For successful responses back from a database update the response will be returned back as an Empty response.

managewp-empty-response

Code Snippets Examples

Here is a simple example that can be used to updated an ACF theme option. The $field_value variable would be updated with your new value and the $acf_field_name would be updated to match the name of your ACF field:

// Database
global $wpdb;
$table_prefix = $wpdb->prefix;

// New Value
$field_value = 'New Value';
$acf_field_name = 'simple_example';

// Update ACF Option
$wpdb->update( $table_prefix . 'options', array( 'option_value' => $field_value ), array( 'option_name' => $acf_field_name ) );

 

And here is an example that is a bit more advanced. This is the snippet that we use to update the caption and credits link that appears on all of our projects:

// Database
global $wpdb;
$table_prefix = $wpdb->prefix;

// Settings
$caption = 'Web Design by'; // Appears in front of the link
$company_name = 'nvision';
$nofollow = 'nofollow'; // Change to '' if you don't want nofollow
$title = 'Digital Marketing & Web Design Company'; // Shows when hovering over the link
$url = 'https://nvision.co/?utm_source='.$_SERVER['SERVER_NAME'].'&utm_medium=client_backlink';

// Credit Link
$new_content = $caption . ' <a title="' . $title . '" href="' . $url . '" target="_blank" rel="noopener ' . $nofollow . '">' . $company_name . '</a>';

// Update ACF Option
$wpdb->update( $table_prefix . 'options', array( 'option_value' => $new_content ), array( 'option_name' => 'options_sitecredits' ) );

 

Big Gulps, Huh?

big-gulps

As you may have noticed, this scenario is very niche and won’t come up all too often. But if you anticipate that this could potentially come up in the future, you may want to prepare your setup ahead of time so that you can utilize this feature and save yourself LOTS of time down the road.

Read More

Read More About

Book with us

Let’s accomplish what you are looking for, our team of experts are here for you.

Let's work together

Warning: Working with our team may result in excessive creativity, uncontrollable 'aha' moments, and an addiction to perfect pixels. Please proceed with caution.