include Max mega menu into theme - pitfalls & solutions

Einfach Dinge, die nichts mit XAMPP, Apache Friends, Apache, MySQL, PHP und alle dem zu tun haben. Allerlei halt. ;)

include Max mega menu into theme - pitfalls & solutions

Postby unleash_it » 01. January 2020 23:42

include Max mega menu Plugin into theme - pitfalls and possible solutions

dear experts

with a new project i have various plugins on my list - wich i want to include on my wordrpess-site;

i need to customize and tweak the site - and also the theme and the menu: I want to add the Max Mega Menu plugin into my theme for a project.

What i've done so far:

- installed the plugins like wp-job-manager, polylang, participants-database, etc. etx.
- copy and paste plugin folder into my theme
- include megamenu.php file into my theme function.php file
- needed to work out further steps and action that has to be done

include with theme:
- active when theme active
- the question that arises: how can I include this plugin and activate it when my theme is activated?
plugins theme-development menus themes

but while i digged deeper into this topic i saw that there are some pitfalls: i found out some intersting things:

- this is a conflict with template.
- i cannot just simply include a plugin into my theme by calling it from the functions.php.

The reason for possible conflicts: is that if the user already has the same plugin installed, this will lead to a fatal error and will cause some ugly errors. Also, we shouldn't move parts of the plugin to the theme, because we don't know how the plugin works internally. It might be looking for a file that isn't there and throw an error and things alike.

conclusio: the right way to do this is have the complete plugin in a subdirectory of the theme.

the solution: i have to do some checks:

- checked whether it hasn't already be loaded.
- This step can be taken over by checking the existence of a certain function or class that you
know is available in the plugin. Sample code:


Code: Select all
add_action('after_setup_theme', 'wpse555555_load_plugin');
function wpse555555_load_plugin() {
    if (!class_exists('wpse555555')) {
        include_once (get_template_directory() . 'plugins/wpse555555/wpse555555.php');
        }
    }


but beware: there are still various pitfalls. we can try solving these ourself or use a PHP library like TGMPA
that will manage this for us.

see this helplink:
a. https://code.tutsplus.com/tutorials/usi ... -cms-20901
b. https://github.com/TGMPA/TGM-Plugin-Activation

The simplest way to get round this, is to check the #TGMPA-Status:
a download of the current version of the library from http://tgmpluginactivation.com/download/ (for our purposes,
just click on the “Zipball” link below the form) Unzip the file and

- Copy/upload (via FTP) just the class-tgm-plugin-activation.php file to the /public_html/wp-content/themes/xx/includes/plugins/class-tgm-plugin-activation.php directory of the website, replacing the existing class-tgm-plugin-activation.php file.
- Once we’ve done that, all should be fine again.

and all the following quots are taken from this site: https://code.tutsplus.com/tutorials/usi ... -cms-20901

All the same, we have to make sure to check the state and to update the TGMPA file they include with their theme. the download of a fresh copy of TGMPA using the Custom TGMPA Generator can help here, which will serve with a customized download of TGMPA which will automagically have done the search & replace the correct way. TGM Plugin Activation is a PHP library that allows you to easily require or recommend plugins for your WordPress themes (and plugins). It allows your users to install, update and even automatically activate plugins in singular or bulk fashion using native WordPress classes, functions and interfaces. You can reference bundled plugins, plugins from the WordPress Plugin Repository or even plugins hosted elsewhere on the internet.


Features
The TGM Plugin Activation library revolutionizes how plugins can be handled with WordPress themes and other plugins. By using classes that are utilized within WordPress, the TGM Plugin Activation library can automatically install, update and activate multiple plugins that are either bundled with a theme, downloaded from the WordPress Plugin Repository or downloaded elsewhere on the internet (perhaps a private repository).The library uses the WP_Filesystem Abstraction class to find the best way to install the plugins - WP_Filesystem searches through a number of methods (Direct, FTP, FTP Sockets, SSH) and determines the best one to use based
on the user’s server setup. If any FTP credentials are needed, a form will be displayed to prompt users to input their FTP credentials in order to continue processing the request. The library uses WordPress’ own Plugin_Upgrader and Plugin_Installer_Skin and extensions of other WordPress upgrader classes to handle singular and bulk installations.


i found some help on this helplink: https://code.tutsplus.com/tutorials/usi ... -cms-20901

Installing TGM Plugin Activation is ridiculously easy. Just follow these steps: Download the TGM Plugin Activation library from the "Download" section of the page. Open the zip file and extract class-tgm-plugin-activation.php into your theme folder (anywhere you like). Open your theme's functions.php file and use the require_once() function to, well, require the class file (once) in your theme. Create a function to configure TGM Plugin Activation and hook it to tgmpa_register via the add_action() function. Done! It's so easy that you don't even need complicated PHP code in order to require or recommend plugins. Take a look at the code below:

Code: Select all
<?php
/**
 * Since I'm already doing a tutorial, I'm not going to include comments to
 * this code, but if you want, you can check out the "example.php" file
 * inside the ZIP you downloaded - it has a very detailed documentation.
 */
require_once dirname( __FILE__ ) . '/class-tgm-plugin-activation.php';
 
add_action( 'tgmpa_register', 'mytheme_require_plugins' );
 
function mytheme_require_plugins() {
 
    $plugins = array( /* The array to install plugins */ );
    $config = array( /* The array to configure TGM Plugin Activation */ );
 
    tgmpa( $plugins, $config );
}
 
?>


From now on, you can make your users install new plugins by setting up the $plugins variable in the function you just created. Let's see how it's done.
Installing Plugins with TGM Plugin Activation: As you can see from above, the $plugins variable is an array. And to define plugins to install, you need to create arrays within that array (so you can set their own parameters). Sounds hard, but it's not:


Code: Select all
<?php
 
$plugins = array(
    array( /* my first plugin */ ),
    array( /* my second plugin */ ),
    array( /* my third plugin */ ),
    // ...
    array( /* my nth plugin */ )
);
 
?>


There are a couple of parameters to use:

name (string, required) - The name of the plugin.
slug (string, required) - The slug of the plugin (usually its folder's name).
required (boolean, required) - If it's set to true, your theme will "require" the plugin. If false, the theme will "recommend" it.
source (string, sometimes required) - The source of the plugin. If it's a WordPress.org plugin, this parameter shouldn't be used; else, it's required.
version (string, optional) - The minimum version required for the plugin. For example; if the theme user has already installed a required plugin but doesn't have the minimum version number you specified, TGM Plugin Activation warns the user to update it.
force_activation (boolean, optional) - If set to true, the user will not be able to deactivate the plugin while your theme is active. A bit annoying but it might be necessary in some scenarios. force_deactivation (boolean, optional) - If set to true, the plugin will be deactivated once the user switches themes.
external_url (string, optional) - If set, the name of the plugin will be linked to this address in the plugin requirement notice.You have three options to make your users install plugins with TGM Plugin Activation: You can require a plugin from the WordPress Plugin Directory, from an external source (like your own server or a CDN),
or from your theme folder (like /my-theme/plugins/shortcodes.zip). Requiring a Plugin From WordPress.org


Code: Select all
<?php
$plugins = array(
    array(
        'name'      => 'BuddyPress',
        'slug'      => 'buddypress',
        'required'  => false, // this plugin is recommended
    )
);
 
?>


Requiring a Plugin From an External Source

Code: Select all
<?php
 
$plugins = array(
    array(
        'name'               => 'My Awesome Plugin',
        'slug'               => 'my-awesome-plugin',
        'source'             => 'http://files.my-website.com/my-awesome-plugin.zip',
        'required'           => true, // this plugin is required
        'external_url'       => 'http://my-website.com/introducing-my-awesome-plugin', // page of my plugin
        'force_deactivation' => true, // deactivate this plugin when the user switches to another theme
    )
);
 
?>


Requiring a Plugin From the Theme Directory

Code: Select all
<?php
 
$plugins = array(
    array(
        'name'               => 'My Super Sleek Slider',
        'slug'               => 'my-super-sleek-slider',
        'source'             => get_stylesheet_directory() . '/lib/plugins/my-super-sleek-slider.zip', // The "internal" source of the plugin.
        'required'           => true, // this plugin is required
        'version'            => '1.2', // the user must use version 1.2 (or higher) of this plugin
        'force_activation'   => false, // this plugin is going to stay activated unless the user switches to another theme
    )
);
 
?>



and all the above mentioned quotes are taken from this site: https://code.tutsplus.com/tutorials/usi ... -cms-20901

i only wanted to share the insights with you. i hope some of you may find it helpful

well - i run into some issues - and after googling and digging deeper into all the things
for Wordpress-development :: super toolkits a. http://wpgear.org/ :: and b. github.com/miziomon/awesome-wordpress
:: Awesome WordPress: A curated list of amazingly awesome WordPress resources, themes, plugins and shiny things.
User avatar
unleash_it
 
Posts: 174
Joined: 10. December 2011 18:32
Operating System: linux opensuse 12.1

Return to Allerlei

Who is online

Users browsing this forum: No registered users and 2 guests

cron