Automatická instalace pluginů vyžadovaných pro šablonu

Pokud vytváříte vlastní šablony nebo upravujete šablony ve wordpressu, které potřebují instalovat pluginy, tak určitě využijete TGM plugin activation.

Určitě jste se dostali do situace, kdy jste museli zákazníkovy, nebo komukoliv jinému, kdo chce vaši šablonu použít, že aby se mu ty obrázky hýbali stejně jako na ukázce, musí si nainstalovat určité pluginy a on s wordpressem neumí. Mám pro vás tip na zajímavou třídu, pomocí které jednoduše rozšíříte funkčnost vaší šablony o upozornění na nutnost instalovat potřebné pluginy.

Třída se jmenuje TGM plugin activation a najdete ji na Github https://github.com/thomasgriffin/TGM-Plugin-Activation.

Pro přidání této funkčnosti nemusíte stahovat zip soubor, ale stačí jen do složky šablony umístit soubor class-tgm-plugin-activation.php. Předpokládám, že většina z vás chce mít v souborech pořádek, tak má v šabloně složku například library, kam takové soubory umísťuje. Na Githubu je také soubor examples, kde je popsána implementace třídy a který si doporučuji projít.

Vložení třídy a nastavení

do souboru functions.php musíme nejprve třídu vložit, proto do něj přidáme následující řádek:

require_once dirname( __FILE__ ) . '/library/class-tgm-plugin-activation.php';

Upravte cestu, podle toho, kde máte plugin umístěn.

Dále musíme zaregistrovat funkci, pomocí které budeme třídu aktivovat:

add_action( 'tgmpa_register', 'my_theme_register_required_plugins' );

Vytvoříme si vlastní funkci:

function my_theme_register_required_plugins() {
 $plugins = array(
 array(
 'name' => 'TGM Example Plugin', // The plugin name.
 'slug' => 'tgm-example-plugin', // The plugin slug (typically the folder name).
 'source' => get_stylesheet_directory() . '/lib/plugins/tgm-example-plugin.zip', // The plugin source.
 'required' => true, // If false, the plugin is only 'recommended' instead of required.
 'version' => '', // E.g. 1.0.0. If set, the active plugin must be this version or higher.
 'force_activation' => false, // If true, plugin is activated upon theme activation and cannot be deactivated until theme switch.
 'force_deactivation' => false, // If true, plugin is deactivated upon theme switch, useful for theme-specific plugins.
 'external_url' => '', // If set, overrides default API URL and points to an external URL.
 ),
 array(
 'name' => 'TGM New Media Plugin', // The plugin name.
 'slug' => 'tgm-new-media-plugin', // The plugin slug (typically the folder name).
 'source' => 'https://s3.amazonaws.com/tgm/tgm-new-media-plugin.zip', // The plugin source.
 'required' => true, // If false, the plugin is only 'recommended' instead of required.
 'external_url' => 'https://github.com/thomasgriffin/New-Media-Image-Uploader', // If set, overrides default API URL and points to an external URL.
 ),
 array(
 'name' => 'BuddyPress',
 'slug' => 'buddypress',
 'required' => false,
 ),
 );
 $config = array(
 'default_path' => '',
 'menu' => 'tgmpa-install-plugins',
 'has_notices' => true,
 'dismissable' => true,
 'dismiss_msg' => '',
 'is_automatic' => false,
 'message' => '',
 'strings' => array(
 'page_title' => __( 'Install Required Plugins', 'tgmpa' ),
 'menu_title' => __( 'Install Plugins', 'tgmpa' ),
 'installing' => __( 'Installing Plugin: %s', 'tgmpa' ), // %s = plugin name.
 'oops' => __( 'Something went wrong with the plugin API.', 'tgmpa' ),
 'notice_can_install_required' => _n_noop( 'This theme requires the following plugin: %1$s.', 'This theme requires the following plugins: %1$s.' ), // %1$s = plugin name(s).
 'notice_can_install_recommended' => _n_noop( 'This theme recommends the following plugin: %1$s.', 'This theme recommends the following plugins: %1$s.' ), // %1$s = plugin name(s).
 'notice_cannot_install' => _n_noop( 'Sorry, but you do not have the correct permissions to install the %s plugin. Contact the administrator of this site for help on getting the plugin installed.', 'Sorry, but you do not have the correct permissions to install the %s plugins. Contact the administrator of this site for help on getting the plugins installed.' ), // %1$s = plugin name(s).
 'notice_can_activate_required' => _n_noop( 'The following required plugin is currently inactive: %1$s.', 'The following required plugins are currently inactive: %1$s.' ), // %1$s = plugin name(s).
 'notice_can_activate_recommended' => _n_noop( 'The following recommended plugin is currently inactive: %1$s.', 'The following recommended plugins are currently inactive: %1$s.' ), // %1$s = plugin name(s).
 'notice_cannot_activate' => _n_noop( 'Sorry, but you do not have the correct permissions to activate the %s plugin. Contact the administrator of this site for help on getting the plugin activated.', 'Sorry, but you do not have the correct permissions to activate the %s plugins. Contact the administrator of this site for help on getting the plugins activated.' ), // %1$s = plugin name(s).
 'notice_ask_to_update' => _n_noop( 'The following plugin needs to be updated to its latest version to ensure maximum compatibility with this theme: %1$s.', 'The following plugins need to be updated to their latest version to ensure maximum compatibility with this theme: %1$s.' ), // %1$s = plugin name(s).
 'notice_cannot_update' => _n_noop( 'Sorry, but you do not have the correct permissions to update the %s plugin. Contact the administrator of this site for help on getting the plugin updated.', 'Sorry, but you do not have the correct permissions to update the %s plugins. Contact the administrator of this site for help on getting the plugins updated.' ), // %1$s = plugin name(s).
 'install_link' => _n_noop( 'Begin installing plugin', 'Begin installing plugins' ),
 'activate_link' => _n_noop( 'Begin activating plugin', 'Begin activating plugins' ),
 'return' => __( 'Return to Required Plugins Installer', 'tgmpa' ),
 'plugin_activated' => __( 'Plugin activated successfully.', 'tgmpa' ),
 'complete' => __( 'All plugins installed and activated successfully. %s', 'tgmpa' ), // %s = dashboard link.
 'nag_type' => 'updated' // Determines admin notice type - can only be 'updated', 'update-nag' or 'error'.
 )
 );

 tgmpa( $plugins, $config );
}
  • Name je jméno pluginu,
  • slug je url pluginu v administraci,
  • source je url zip souboru pluginu,
  • required znamená, že je plugin vyžadovaný, pokud jej nastavíte na false, bude jen doporučený,
  • version – pokud ji vyplníte, bude vyžadována stejná, nebo vyšší verze pluginu,
  • force_activation znamená, že plugin je aktivovaný se šablonou a nejde deaktivovat, dokud je šablona aktivní,
  • force_deactivation naopak znamená deaktivaci pluginu při deaktivaci šablon.

Pokud jste vše udělali správně, po aktivaci vaší šablony by se mělo zobrazit upozornění na nutnost instalace definovaných pluginů.

tgm-instalace

 

Pokud chcete tento plugin v kompetně v českém jazyce, tak si nás přidejte na facebook a zobrazí se Vám odkaz na stažení aplikace

Stáhnout v CZ verzi

 

 

(241)