programmazione Plugin programmare wordpress
Per poterti collegare ad un file che si trova dentro ad un tuo plugin, si usano delle funzioni di wordpress e sono:
plugin_basename( __FILE__ ); | Ottiene il nome di base di un plug-in. |
plugin_dir_path( __FILE__ ); | Ottieni il percorso della directory del filesystem (con barra finale) per il plugin FILE passato. |
plugin_dir_path( __FILE__ ) . ‘includes/include-test.php’ | Recupera il percorso della directory del filesystem del file include-test.php che si trova nella cartella includes/ |
plugins_url(); | Recupera un URL all’interno della directory dei plugin o mu-plugin. |
plugins_url( ‘includes’, __FILE__ ); | Recupera un URL all’interno della directory dei plugin nella directory /includes |
plugin_dir_url( __FILE__ ); | Ottieni il percorso della directory URL (con barra finale) per il plug-in FILE passato. |
esempio:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
?> <div class="wrap"> <h1><?php esc_html_e( get_admin_page_title() ); ?></h1> <?php $wpplugin_plugin_basename = plugin_basename( __FILE__ ); $wpplugin_plugin_dir_path = plugin_dir_path( __FILE__ ); $wpplugin_plugins_url_default = plugins_url(); $wpplugin_plugins_url = plugins_url( 'includes', __FILE__ ); $wpplugin_plugin_dir_url = plugin_dir_url( __FILE__ ); ?> <ul> <li>plugin_basename( __FILE__ ) - <?php echo $wpplugin_plugin_basename; ?></li> <li>plugin_dir_path( __FILE__ ) - <?php echo $wpplugin_plugin_dir_path; ?></li> <li>plugins_url() - <?php echo $wpplugin_plugins_url_default; ?></li> <li>plugins_url( 'includes', __FILE__ ) - <?php echo $wpplugin_plugins_url; ?></li> <li>plugin_dir_url( __FILE__ ) - <?php echo $wpplugin_plugin_dir_url; ?></li> <li>included file test - <?php include( plugin_dir_path( __FILE__ ) . 'includes/include-test.php'); ?></li> </ul> </div> <?php |
codice file /includes/include-test.php:
1 2 3 |
<?php $wpplugin_plugins_url = plugins_url( '/', __FILE__ ); echo $wpplugin_plugins_url; |
risultato:
- plugin_basename( __FILE__ ) – 6.02.02-admin-footer-credits-folder/6.07-plugin-file-paths.php
- plugin_dir_path( __FILE__ ) – /www/wordpress_https/wp-content/plugins/6.02.02-admin-footer-credits-folder/
- plugins_url() – https://mstc.sytes.net/wp-content/plugins
- plugins_url( ‘includes’, __FILE__ ) – https://mstc.sytes.net/wp-content/plugins/6.02.02-admin-footer-credits-folder/includes
- plugin_dir_url( __FILE__ ) – https://mstc.sytes.net/wp-content/plugins/6.02.02-admin-footer-credits-folder/
- included file test – https://mstc.sytes.net/wp-content/plugins/6.02.02-admin-footer-credits-folder/includes/