I am building a WordPress plugin to show documentation related to the project. There is a good amount of HTML required in the views, with the content coming from markdown files.
I was wondering if it's at all possible to use Timber Plugin for admin screens, to help keep everything clean? I cannot find anything in the docs.
I have a function that renders a view (called by add_menu_page()) which is working fine. If I echo some text or HTML or even echo the contents of a file, I see what I'd expect.
Attempting to use Timber in the same way as I would in a theme, like this:
$context = Timber::get_context();
Timber::render(plugin_dir_url( __DIR__ ) . 'views/docs.twig', $context);
This just gives me an empty page, with no errors thrown at all.
Do I need to initialise Timber in some way first?
This is the first plugin I have made and my PHP isn't brilliant so apologies if this question isn't easy to follow or understand.
According to the Template Locations documentation, by default Timber looks in the views/ directory of the current theme for the template files. You can add other paths this way:
Timber\Timber::$locations = array(
plugin_dir_path( __FILE__ ) . 'views/'
);
Timber will look in that location first, so templates in the plugin will override any in the theme if they share the same name.
Yes! Just figured it out.
So, on your functions.php, do the usual procedure of adding a page on the admin side
prepare the function:
function documents_menu() {
add_menu_page('Docs', 'Docs', 'manage_options', '', 'myplguin_admin_page', 'dashicons-heart', 6);
}
then the hook:
add_action( 'admin_menu', 'documents_menu' );
then the renderer function:
function myplguin_admin_page(){
if ( ! class_exists( 'Timber' ) ) {
// if you want to show some error message, this is the right place
echo "Timber doesn't exist!";
return;`enter code here`
}
here's the key part:
Timber::render( 'docs.html.twig', array(
'args' => $args,
'instance' => $instance,
/* any other arguments */
));
}
then on your [theme]/views, create docs.html.twig and create your twig file!
Related
I use a basic custom plugin I have built for various WordPress admin functions.
One of them adds a custom CSS file that hooks into the admin as per the example below:
// Sets A Custom Admin Colour Scheme
function hits_admin_colour_scheme() {
wp_register_style('hits_admin_colour_scheme', plugins_url('/assets/css/admin.css',__FILE__ ));
wp_enqueue_style('hits_admin_colour_scheme');
}
add_action( 'admin_init','hits_admin_colour_scheme');
Is it possible to have an additional stylesheet that is only loaded if a particular plugin is installed? Say WooCommerce or ACF? This way I can reduce the size of the default CSS file and only load what is relevant.
Sorry if this is a newbie question. I appreciate any help.
You can check whether woocommerce(or others by same way) is activated before enqueue the file
function hits_admin_colour_scheme() {
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
wp_register_style('hits_aditional_colour_scheme', plugins_url('/assets/css/additional.css',__FILE__ ));
wp_enqueue_style('hits_aditional_colour_scheme');
}
}
add_action( 'admin_enqueue_scripts', 'hits_admin_colour_scheme');
and please use admin_enqueue_scripts hook to enqueue admin assets.
I am trying to override woo commerce single product template but it won't get overridden.
I have done the following steps:
Added file inside my theme directory.
Added support for Woo-commerce in my functions.php file
add_action( 'after_setup_theme', 'wpse319485_add_woocommerce_support' );
function wpse319485_add_woocommerce_support() {
add_theme_support( 'woocommerce' );
}
Made sure the debug mode is off in config file
define( 'WC_TEMPLATE_DEBUG_MODE', false );
Checked the status of templates over-riden
Added an h1 tag with test text in it.
I even tried deleting everything but the default template is loaded.
What am I missing ?
I'm not sure if you are still facing this problem. I ran into the very same. After a very frustrating day, tempted to hack the plugin files, I ended up adding woocommerce.php to my theme, containing:
if ( is_product() ) {
wc_get_template( 'woocommerce/single-product.php' );
}else{
//For ANY product archive.
//Product taxonomy, product search or /shop landing
wc_get_template( 'woocommerce/archive-product.php' );
}
This is an adaptation of what I found here:
https://wordpress.org/support/topic/archive-productphp-template-overwrite-not-working/
It did not work without adding
woocommerce/
I'm using the WordPress plugin Ultimate member which has a hook called um_user_register which should happen after a user fills out a registration form. According to the documentation (https://docs.ultimatemember.com/article/1308-umuserregister) I should be able to use that hook and do my thing. Unfortunately nothing happens.
The code I have added to my theme's functions.php file:
//Ultimate Member - Extra stuff after user registers
add_action( 'um_user_register', 'my_user_register', 10, 2 );
function my_user_register( $user_id, $args ) {
add_user_meta( $user_id, 'user_utlimate_member_signup', 'Yes');
}
Am I putting my add_action or function in the wrong place or is there something I'm overlooking?
I'm currently developing a plugin for wordpress.
My plugin-content gets fired on defined shortcode. I pass a parameter to the shortcode to make some conditionals. I am able to load JS conditionally, but I also need to load CSS conditionally.
Lets say I use the shortcode: [myshortcode css="dark"]
I make a database query and inject the wanted css.
Is this somehow possible?
I have read some threads about it. I know it is because the Code fires after head is loaded. I wasn't able to find a solution.
What options do I have?
Thank you!
Probably you are searching for these functions:
has_shortcode()
get_shortcode_regex() - here you can find nice example, which is close to your request
You can check your post, page or custom post type on hook by add_action ( 'wp', 'yourCustomFunction' ) and test if your get_post_field ( 'post_content' ) contains specified shortcode and conditionally enqueue CSS file based on specified attribute.
There is a better way. You can simply register your css and scripts with wp_enqueue_scripts by wp_register_style or wp_register_script and then enqueue the registered scripts from your shortcode. You will have opportunity to enqueue scripts based on certain condition there.
Here is a code example.
/**
* Register scripts
*/
function my_plugin_scripts() {
wp_register_style( 'dark-css', $css_path );
wp_register_script( 'script-name', $js_path, array(), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'my_plugin_scripts' );
/**
* My Shortcode
*/
function my_plugin_shortcode( $atts ) {
$atts = shortcode_atts( array(
'css' => 'default'
), $atts );
if ( $atts['css'] == 'dark') {
wp_enqueue_style('dark-css')
}
// do shortcode actions here
}
add_shortcode( 'shortcode-id','my_plugin_shortcode' );
If I understand your question correctly, you could do something like this to load one stylesheet or the other:
function aw_shortcode_function($atts) {
$a = shortcode_atts( array(
'css' => 'light', // this is the default value
), $atts );
$colorTheme = $a['css'];
if ($colorTheme == 'dark') {
// load/enqueue/get/do whatever based on the css="dark" shortcode attribute
}
}
I am developing a plugin everything is going fine but there is a problem.
How can I hide or remove the comments box? I lost many hours but could not develop the right script. My current script is this:
add_filter( 'comments_template', 'remove_comments_template_on_pages', 11 );
function remove_comments_template_on_pages( $file ) {
if ( is_page() )
comments_template('', true);
}
I don't know how to develop this script. Any help could lead me to complete my plugin.
Add the below code to you functions.php file.
add_action('init', 'remove_page_feature');
function remove_page_feature() {
remove_post_type_support( 'page', 'comments' );
}
And now provide a check before the comments template is executed, as below:
if ( post_type_supports( 'page', 'comments' ); )
comments_template('', true);
}
Note: Using the above code will disable comments on pages.