{"id":2903,"date":"2016-04-27T05:50:43","date_gmt":"2016-04-27T05:50:43","guid":{"rendered":"http:\/\/musilda.cz\/?p=2903"},"modified":"2016-04-27T05:50:43","modified_gmt":"2016-04-27T05:50:43","slug":"jak-zajistit-aby-se-vas-wordpress-plugin-nahral-jako-posledni","status":"publish","type":"post","link":"https:\/\/affinite.io\/cs\/jak-zajistit-aby-se-vas-wordpress-plugin-nahral-jako-posledni\/","title":{"rendered":"Jak zajistit, aby se v\u00e1\u0161 WordPress plugin nahr\u00e1l jako posledn\u00ed"},"content":{"rendered":"

Tento snippet, podobn\u011b, jako ostatn\u00ed, vznikl z pot\u0159eby vy\u0159e\u0161en\u00ed probl\u00e9mu. Konkr\u00e9tn\u011b bylo nutn\u00e9 ze \u0161ablony p\u0159en\u00e9st funkce do pluginu a zajistit, aby se nahr\u00e1l, jako posledn\u00ed.<\/p>\n

Pro\u010d posledn\u00ed?<\/p>\n

Proto\u017ee, pokud p\u00ed\u0161ete plugin, jen\u017e m\u00e1 upravovat fungov\u00e1n\u00ed, nebo zobrazov\u00e1n\u00ed jin\u00e9ho pluginu, m\u016f\u017ee se st\u00e1t, \u017ee glob\u00e1ln\u00ed prom\u011bnn\u00e9, nebo konstanty, nejsou dostupn\u00e9.<\/p>\n

Tento p\u0159\u00edpad nastane, pokud je v\u00e1\u0161 plugin na\u010dten d\u0159\u00edve, ne\u017e ten „zdrojov\u00fd“.<\/p>\n

Aby jsme n\u00e1\u0161 plugin na\u010detli posledn\u00ed, mus\u00edme zm\u011bnit pole, v option\u00a0active_plugins.<\/p>\n

Nejprve vytvo\u0159\u00edme path na\u0161eho pluginu:<\/p>\n

$path = str_replace( WP_PLUGIN_DIR . '\/', '', __FILE__ );<\/pre>\n

WordPress toti\u017e ukl\u00e1d\u00e1 aktivn\u00ed plugin do pole, kde hodnoty pole jsou slo\u017eka pluginu + n\u00e1zev hlavn\u00edho souboru pluginu:<\/p>\n

muj-plugin\/muj-plugin.php<\/pre>\n

Nyn\u00ed mus\u00edme na\u010d\u00edst seznam aktivn\u00edch plugin\u016f:<\/p>\n

$plugins = get_option( 'active_plugins' );<\/pre>\n

Proto\u017ee v\u00edm, \u017ee plugin je aktivn\u00ed a je ulo\u017een v poli, najdeme jeho kl\u00ed\u010d:<\/p>\n

$key = array_search( $path, $plugins );<\/pre>\n

Proto\u017ee jsme z\u00edskali kl\u00ed\u010d, z\u00e1znam sma\u017eeme:<\/p>\n

array_splice( $plugins, $key, 1 );<\/pre>\n

Vezmeme n\u00e1\u0161 $path a „p\u0159ilep\u00edme“ ho na konec pole, jako posledn\u00ed z\u00e1znam:<\/p>\n

$plugins[] = $path;<\/pre>\n

A nakonec p\u0159eulo\u017e\u00edme option:<\/p>\n

update_option( 'active_plugins', $plugins );<\/pre>\n

Cel\u00e1 funkce pak vypad\u00e1 n\u00e1sledovn\u011b:<\/p>\n

function my_plugin_load_last()\n{\n   $path = str_replace( WP_PLUGIN_DIR . '\/', '', __FILE__ );\n     if ( $plugins = get_option( 'active_plugins' ) ) {\n\n        if ( $key = array_search( $path, $plugins ) ) {\n\n           array_splice( $plugins, $key, 1 );\n           $plugins[] = $path;\n           update_option( 'active_plugins', $plugins );\n\n        }\n    }\n}<\/pre>\n

A samoz\u0159ejm\u011b, mus\u00edme funkci „zav\u011bsit“ do p\u0159\u00edslu\u0161n\u00e9ho hooku:<\/p>\n

add_action( 'activated_plugin', 'my_plugin_load_last' );<\/pre>\n

T\u00edm jsme doc\u00edlil, \u017ee se n\u00e1\u0161 plugin na\u010dte a\u017e \u00fapln\u011b posledn\u00ed.<\/p>\n

 <\/p>\n","protected":false},"excerpt":{"rendered":"

Tento snippet, podobn\u011b, jako ostatn\u00ed, vznikl z pot\u0159eby vy\u0159e\u0161en\u00ed probl\u00e9mu. Konkr\u00e9tn\u011b bylo nutn\u00e9 ze \u0161ablony p\u0159en\u00e9st funkce do pluginu a zajistit, aby se nahr\u00e1l, jako posledn\u00ed. Pro\u010d posledn\u00ed? Proto\u017ee, pokud p\u00ed\u0161ete plugin, jen\u017e m\u00e1 upravovat fungov\u00e1n\u00ed, nebo zobrazov\u00e1n\u00ed jin\u00e9ho pluginu, m\u016f\u017ee se st\u00e1t, \u017ee glob\u00e1ln\u00ed prom\u011bnn\u00e9, nebo konstanty, nejsou dostupn\u00e9. Tento p\u0159\u00edpad nastane, pokud je<\/p>\n","protected":false},"author":1,"featured_media":2635,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[19],"tags":[286,460,517],"class_list":["post-2903","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-snippety","tag-plugin-load","tag-wordpress-2","tag-wordpress-snippet"],"_links":{"self":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/posts\/2903"}],"collection":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/comments?post=2903"}],"version-history":[{"count":0,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/posts\/2903\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/media\/2635"}],"wp:attachment":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/media?parent=2903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/categories?post=2903"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/tags?post=2903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}