Using timber to render plugin pages - wordpress

I am making a plugin which adds a bunch of pages to the admin view of WP. I'd really like to use Timber, specifically, the Twig templating functionality to render these pages.
While I have next to zero experience in WP and PHP in general, what attracts me to this approach is my previous familiarity with Django / Flask templates which allow me to extend a base template and specify blocks for header, content, footer. That seems trivial to do with Timber when using it to create a theme, but I can't for the life of me figure out how to make this setup work within a plugin. Sure, I can do something like this:
add_action( 'admin_menu', 'test_setup_menu' );
function test_setup_menu() {
add_menu_page(
'Tables',
'Tables',
'manage_options',
'test-tables',
'admin_page_test'
);
}
function admin_page_test() {
Timber::Render( 'test.twig');
}
But that of course will render test.twig with header and footer parts already populated from the theme. The issue specifically is that I want to be able to add information to the header or footer blocks. I know I can do this like so:
add_action('admin_head', 'add_to_head')
function add_to_head() {
...
}
But this is precisely the type of thing I'm trying to avoid, I wish to encapsulate this type of logic in a Twig template. Is there any way to make this work?

Here's an example of how to add a custom admin page for a plugin.
<?php
/**
* Plugin Name: Test Run
*/
add_action('admin_menu', 'admin_menu_cb');
function admin_menu_cb()
{
// Ref: https://developer.wordpress.org/reference/functions/add_menu_page/
add_menu_page('Test Run Admin Page', 'Test Run', 'manage_options', 'test-run', 'render_menu_page_cb', 'dashicons-schedule', 3);
}
function render_menu_page_cb()
{
Timber::$locations = __DIR__.'/views';
$data = [];
Timber::render('main.twig', $data);
}
For a more full example please see the below repo. I created it recently as a guide for anyone to use Timber in a wordpress plugin.
https://github.com/chanakasan/a-wordpress-plugin-using-timber

Related

How I can print all variable of a hook in drupal 8?

I'm very new in Drupal 8 and I have issue now with hook. Mainly I though that I don't clearly understand structure and hook definition in Drupal 8.
So my main problem is that I have some hook to interact with main menu (add custom class name to ul, li and link, a tag). I can do it by changing template file and now try to do it with any hook.
Although I found that some hook relating to menu ex. hook_contextual_links_alter (link: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Menu%21menu.api.php/function/hook_contextual_links_alter/8.9.x).
At the end of this hook we have the code related:
function hook_contextual_links_alter(array &$links, $group, array $route_parameters) {
if ($group == 'menu') {
// Dynamically use the menu name for the title of the menu_edit contextual
// link.
$menu = \Drupal::entityTypeManager()
->getStorage('menu')
->load($route_parameters['menu']);
$links['menu_edit']['title'] = t('Edit menu: #label', [
'#label' => $menu
->label(),
]);
}
}
So I have installed devel module with kint function and in my .theme file and try:
function hook_contextual_links_alter(array &$links, $group, array $route_parameters) {
kint($links);
}
and then reload my Home page but nothing showed. But I can get some information about other like:
function eg_learn_theme_suggestions_page_alter(&$suggestions, $variables) {
kint($suggestions);
}
So what happens here? Can you help to explain if how I can print the variable of this hook (in .theme file) and the site page to see the printing variable?
In general when I found a hook, how I can print there array and check it in website?
There are some problems about your approach:
When implementing a hook, you must replace "hook" with the module name/theme name where you put the hook function inside. For example, if you want implement hook_contextual_links_alter in your_custom module, it becomes your_custom_contextual_links_alter().
Not all hook can be implemented in the theme. Some hook can only be implemented in modules (in .module file). You can read more here.
In your case, I think hook_preprocess_menu would be more suitable. You can implement it in your custom theme like this:
function <your_theme_name>_preprocess_menu(&$variables) {
if ($variables['menu_name'] == 'main') {
kint($variables);
}
}

wordpress custom page not in admin

I am trying to create a page test.php that can use wordpress functions but is not part of the admin/pages section. so basically if I have a link on my single.php or page.php to direct me to test.php not show the 404 but actually show me the contents of test.php. I've heard that i might have to make some changes to the htaccess but I'm not sure how to search for this issue so any help is greatly appreciated.
You can use include('../wp-load.php'); in the top of your php file to get wordpress functionality in to your file then place your file in the root wordpress installation and then you can call your file with your url http://yourdomain.net/test.php
The information for creating page templates, plugins and using functions.php can all be found on the WordPress site.
How to call wordpress functions in custom php script
if ( is_admin() ) {
add_action( 'admin_menu', array( 'adminAddPage' ) );
}
/**
* Callback to add page
*/
public function adminAddPage() {
add_options_page( 'Custom page title', 'Custom page title', 'manage_options', 'custom_page_slug', array('adminPage') );
}
/**
* Page HTML Callback
*/
public function adminPage() {
// your html code
}

Replacing only parts of an archive and single page template of WordPress

I am having a bit of trouble here understanding how to do the following. I have searched for weeks now but cannot seem to find what I am looking for.
I have a custom post type 'product' and want to change which template gets loaded for the single product page as well as the archive for the products. I am using the following code to load include and load templates.
add_filter('template_include', function() {
if (is_post_type_archive('product')) {
$templatefilename = 'archive-product.php';
$template = WPVS_PATH . 'templates/' . $templatefilename;
return $template;
}
if ('product' == get_post_type() ){
$templatefilename = 'single-product.php';
$template = WPVS_PATH . 'templates/' . $templatefilename;
return $template;
}
});
The problem I am having is that it replaces the current theme's template instead of just the inner part of the content and archive areas.
Here is what I want to achieve:
Create a custom post type 'product' in a plugin - DONE (Was kinda easy!)
When opening a single product only change the content part. - I can do this with the_content filter hook. Simple enough. Any other suggestions is welcome.
When I go to the archive view for the 'product' custom post type I don't want to have it load the theme's default archive (list) view but instead a grid view from my plugin which I cannot seem to get right. I only want to change the inner part of the template, not the whole page.
I have created this plugin a few weeks ago using only shortcodes which works good but want to see if I can do it without the use of shortcodes by means of creating the custom post type and changing the inner template parts of the current active theme.
Can anybody steer me into the right direction here?
If I create a theme I can do what I am looking for but I want to create this into a plugin instead without adding or making changes to the active theme. The plugin should handle what is needed.
The same issue is discussed here but what I want is to develop something that is theme independent. No changes should be made in theme files and no theme files should be copied to the plugin.
WP - Use file in plugin directory as custom Page Template?
Recently I also had the same problem. Here's how I worked it out.
template_include filter accepts a parameter which is the selected template that you want to override (this what you are missing in your code).
I don't know but sometimes the filter hook need higher priority to work like 9999. But first check if it work with default priority, if don't change it.
I assume your both archive and single product template both have include get_header() and get_footer() which can be used for default selected theme (Or if the theme has different setup, setup accordingly).
This is simplified code:
add_filter('template_include', function($default_template) {
if (is_post_type_archive('product')) {
$templatefilename = 'archive-product.php';
$template = WPVS_PATH . 'templates/' . $templatefilename;
$default_template = $template;
} else if ('product' == get_post_type() ) {
$templatefilename = 'single-product.php';
$template = WPVS_PATH . 'templates/' . $templatefilename;
$default_template = $template;
}
// Load new template also fallback if both condition fails load default
return $default_template;
}, 9999); // set priority, only if not worked with default one
The best option in this case is to provide a shortcode to the user. So they can place it on any page that they want (or that you auto generate). That way you will place your content inside their theme.
Something like this:
add_shortcode( 'slotsl-game', 'embed_game' );
/**
* Print the game
* #return false|string
*/
function embed_game(){
ob_start();
$game = get_post();
include_once SLOTSL_PLUGIN_DIR . 'templates/slotsl-single-game.php';
return ob_get_clean();
}

remove activity feed from Wordpress admin

I am looking for a way to hide activity feed from the dashboard using a function. Does anyone know how to do this? I want to completely remove it. I want to achieve this without a plugin.
You can use remove_meta_box() like;
function remove_dashboard_widgets(){
remove_meta_box('dashboard_activity', 'dashboard', 'normal');
}
add_action('wp_dashboard_setup', 'remove_dashboard_widgets');
add above code to functions.php
Dashboard widgets and other meta boxes can also be removed by using the unset function. You might need to play around with the array keys, or use var_dump() to find the path for the widget you're looking for.
// Removes dashboard activity widget.
function remove_dashboard_activity_widget() {
global $wp_meta_boxes;
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_activity']);
}
// Triggers dashboard widgets removal.
add_action('wp_dashboard_setup', 'remove_dashboard_activity_widget');
Additionally like Hüseyin BABAL mentioned, meta boxes can also be removed like this:
function remove_dashboard_widgets(){
remove_meta_box('dashboard_activity', 'dashboard', 'normal');
}
add_action('wp_dashboard_setup', 'remove_dashboard_widgets');
You'll either have to create a plugin or add a function to your theme functions.php file.
function remove_activity_dashboard_widget() {
remove_meta_box( 'dashboard_activity', 'dashboard', 'side' );
}
// Hook into the 'wp_dashboard_setup' action to register our function
add_action('wp_dashboard_setup', 'remove_activity_dashboard_widget' );
Here is the codex page on the subject:
http://codex.wordpress.org/Dashboard_Widgets_API#Advanced:_Removing_Dashboard_Widgets

is there a way (plugin) to insert html/css/images ect automatically into every wordpress post?

is there a way (plugin) to insert html/css/images ect.. automatically into every wordpress post? most of my posts are going to be very similar so is there a plugin that will automatically insert the pre written html/css/ ect in every post as opposed to me entering it manually every time.
Thank you in advance ;-)
You can write your own simple function for this, see my example below:
add_filter( 'default_content', 'my_editor_content' );
function my_editor_content( $content ) {
global $post_type;
if( $post_type == 'post') { /* Or your custom post type, pages etc. */
$content = 'Your custom HTML/CSS content here';
}
return $content;
}
Place this in functions.php and it will be the default content of every new post/page/custom post type you create.
For a list of available post types, please refer to the Codex
You could use a plugin such as Ad injection, it will allow you to do what you need without having to alter / amend / ad any code to the templates or files

Resources