DIVI POST SETTINGS INDEX

You are here!

You are here!

You are here!

You are here!

Divi Post Settings Plugin I

How to set the basics of a WordPress and Divi Plugin.
Divi Post Settings Plugin II
Add Settings in Theme Customizer and Save the values.

Divi Post Settings Plugin III

Write our main functions and use our settings.
Divi Post Settings Plugin IV
Add Ajax to our Info notice, Testing and Commenting.

We have talked about the Divi Post Settings metabox and how to save default option values by adding some Php code in your theme (better child theme!) functions.php file; so we thought to go further by writing a WordPress Divi Plugin in a post series.

To recap, in our first series post we have defined what the plugin was supposed to be and to do, we also give the basic file structure and some informations about writing a WordPress plugin. Then we have grounded the plugin itself in our second series post, we added our custom settings in the Theme Customizer and save the values to the database.

Well, now we are going to do the most important part of the job, we have to make sure that when we save an option in the Theme Customizer, then the value saved will be reflected in our post frontend.

So let’s get started!

TO RECAP

Just to resume the row this is at this point our main plugin file:

EVERY ACTION HAS A REACTION

So now we can set our options in the Theme Customizer and get saved the settings directy in the database (calling these ones will be essential for the working of the plugin later!). However, we still have to do much of the work; we have, indeed, to link our settings values to the Divi meta-box options.

COPY THE FUNCTION THAT RETURNS THE META BOX

The first step in order to do it, is simply copy and paste the function “et_single_settings_meta_box” from the functions.php of the Divi theme (should start at line 374).

NOTE: Be careful to copy the if statement wrapping the function (see line 1 and 122) that’s going to check if the same function doesn’t already exist. It’s very important because you can’t redeclare the same function in two places at the same time and doing it you will get a fatal error and the plugin itself will not be activated!

WRAPPING THE OPTIONS IN IF… ELSE STATEMENTS

The core of our plugin will do a simple thing. It let you to save your preferences about the post Layout (Right Sidebar, Left Sidebar or Fullwidth), the Dot Navigation (Off or On), the Hide Nav before scroll (Default, Off or On) and finally about showing the Post Title (Show or Hide).

After that, we have to go in the meta-box function above and move up and down thetags accordingly to the preferences we have saved in the Theme Customizer.

Here come in the rule and the importance of the If… Else statements for our plugin (and for many many others!). In Php the If statement, in all its forms, is the core itself of coding. So we will do something like: let’s say the user go to the Theme Customizer -> Blog -> Divi Post Settings -> Page Layout and select Right Sidebar, then save value ‘Right’ in the database, then recall this value and IF the value = ‘Right’ then show the sidebar on the right, (ELSE) IF instead the value saved = ‘Left’, show it on the left, (ELSE) otherwise show a fullwidth page layout.

Applying this logical reasoning to all our options will give us, in short, the following code:

HOW TO RECALL THEME CUSTOMIZER OPTION VALUES

You should probably have noticed the changes about every ‘Select’ tag; however, in order to check the values saved in the Theme Customizer, we have to call these values before. We have saved these as options, so we have simply to recall them with a get_option() function (see lines 6-9 of the code above) as follows:

IT DOESN’T WORK. SURE, WE HAVE TO SAVE THE OPTIONS!

If you try to test your plugin now, you will see that it seems to work properly, it can be activated without issues, you can find ‘Divi Post Settings’ section under the Blog panel of Theme Customizer, you can also save your preferences and creating a new post you will see these ones already setted in the Divi Post Settings meta-box.

But you’re going to save a post and when you preview it you discover that your options have not been applied at all. The plugin simply doesn’t work!

Sure. We have done much of the work, but a last step is strictly required. we have to save the options!

Divi in his functions.php file do it in a function called ‘et_divi_post_settings_save_details‘ (at line 498); what we have to do is copy and paste this function too in your main plugin file.

However, this time it’s not sufficient to wrap the code in a ‘function_exists’ function; simply you can’t copy this function with the same name. So we have to change the prefix (either in the function name either in the add_action calling) ‘et_’ with what we want (in our case ‘idivi_’) and all is done!

Now our plugin will save properly the options. Try yourself to believe!

LET’S FINISH IT….

A final series post attend we on next tuesday. We will finish the plugin, fix some minor issues and add some extra functionalities.

See you next post. Stay tuned!

DIVI POST SETTINGS INDEX

You are here!

You are here!

You are here!

You are here!

Divi Post Settings Plugin I

How to set the basics of a WordPress and Divi Plugin.
Divi Post Settings Plugin II
Add Settings in Theme Customizer and Save the values.

Divi Post Settings Plugin III

Write our main functions and use our settings.
Divi Post Settings Plugin IV
Add Ajax to our Info notice, Testing and Commenting.

SupportHost

You have Successfully Subscribed!

CodeCanyon Plugins

You have Successfully Subscribed!

Elegant Themes

You have Successfully Subscribed!

Try Divi!

You have Successfully Subscribed!

Divi Plugins

You have Successfully Subscribed!

Advanced Blurbs Plugin

You have Successfully Subscribed!

Bloom!

You have Successfully Subscribed!

Divi Cake Layouts

You have Successfully Subscribed!

Divi Cake Plugins

You have Successfully Subscribed!