Render template wordpress plugin development - wordpress

//settings.php
<div>
<p>Settings</p>
<p><?php echo $text ?></p>
</div>
I render template while developing wordpress plugin using require_once
public function display_admin_page() {
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/templates/settings.php';
}
When I call the function display_admin_page, I can display the template. However I want to pass some args to template, how can I do it. I want to write a function so I can use like this (but I don't know how to write the function
public function display_plugin_admin_page() {
$args = [
'text' => 'Hello'
];
get_plugin_template('admin/templates/settings.php', $args);
}
Could you guy help me how to write get_plugin_template function? Thanks

Related

Localize WooCommerce Extension

I am developing WC extension by its new way. Everything works like a charm instead of localization. I have generated JSON by wp i18n make-json and using in index.js like this:
import { __ } from '#wordpress/i18n';
__( 'Verify licence', 'domain' );
Unfortunatelly the strings are never been translated. Should I load the JSON myself or register the script wp_set_script_translations( 'domain-script', 'domain', plugin_dir_path( __FILE__ ) . '/languages' );
?
At server side translations work. I am really confused about the JS translation flow. Thanks for any help.
For future generations the solution is simple.
You have to call something like this:
public function extension_set_script_translations() {
$loaded = wp_set_script_translations( 'extension', 'extension', plugin_dir_path( __FILE__ ) . '/languages' );
}
private function initLocalizations() {
add_action( 'admin_enqueue_scripts', [$this, 'extension_set_script_translations'] );
}
Another important thing is name of the language JSON file.
{text-domain}-{locale}-{script-handle}.json

Open WooCommerce External Products in New Tab

I'm trying to customize WooCommerce external product links to open in new tabs...
This is my try:
added a filter to the WordPress theme functions.php file as the following:
add_filter( 'woocommerce_product_add_to_cart_url', 'woocommerce_externalProducts_openInNewTab' );
function woocommerce_externalProducts_openInNewTab($product_url) {
global $product;
if ( $product->is_type('external') ) {
$product_url = $product->get_product_url() . '"target="_blank""';
}
return $product_url;
}
What did I missed?
what you're currently doing is wrong... get_product_url is named as what it do. It will give you the url... not the html anchor that has the url, but just the url.. so you're just adding some text to the url.. that's what you are doing...
One solution is given by #Ash Patel. You can change the markup by using templates... just navigate to your plugin folder and look for this file.. woocommerce\templates\single-product\add-to-cart\external.php. You can find instructions inside it.
Now, sometimes, we don't like editing templates... especially if it's just minor edits like this...
Below code will do it the way you want it... just paste this code in your theme's functions.php.
remove_action( 'woocommerce_external_add_to_cart', 'woocommerce_external_add_to_cart', 30 );
add_action( 'woocommerce_external_add_to_cart', 'rei_external_add_to_cart', 30 );
function rei_external_add_to_cart(){
global $product;
if ( ! $product->add_to_cart_url() ) {
return;
}
$product_url = $product->add_to_cart_url();
$button_text = $product->single_add_to_cart_text();
do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<p class="cart">
<?php echo esc_html( $button_text ); ?>
</p>
<?php do_action( 'woocommerce_after_add_to_cart_button' );
}
Here is how you add target="_blank" to the links on archive pages:
function ns_open_in_new_tab($args, $product)
{
if( $product->is_type('external') ) {
// Inject target="_blank" into the attributes array
$args['attributes']['target'] = '_blank';
}
return $args;
}
add_filter( 'woocommerce_loop_add_to_cart_args', 'ns_open_in_new_tab', 10, 2 );
Replace ns_ part with your own namespace abbreviation.
Remove above funtion from function.php:
Use plugin files from Template folder by Template Overwrite method and then
follow below path:
woocommerce\templates\single-product\add-to-cart\external.php
open external.php where there is a tag, apply target="_blank".
it will work.

Is there any way to override a WordPress template with a plugin?

I'd like to make a landing page. If plugin detects some GET or POST requests it should override wordpress theme and show its own.
It would work somehow like that:
if (isset($_GET['action']) && $_GET['action'] == 'myPluginAction'){
/* do something to maintain action */
/* forbid template to display and show plugin's landing page*/
}
I'm familiar with WP Codex, but I don't remember if there is any function to do that. Of course, I googled it with no results.
Thanks for any ideas in advance.
You need the hook template_include. It doesn't seem documented in the Codex, but you can find more examples here in SO or in WordPress StackExchange
Plugin file
<?php
/**
* Plugin Name: Landing Page Custom Template
*/
add_filter( 'template_include', 'so_13997743_custom_template' );
function so_13997743_custom_template( $template )
{
if( isset( $_GET['mod']) && 'yes' == $_GET['mod'] )
$template = plugin_dir_path( __FILE__ ) . 'my-custom-page.php';
return $template;
}
Custom Template in Plugin folder
<?php
/**
* Custom Plugin Template
* File: my-custom-page.php
*
*/
echo get_bloginfo('name');
Result
Visiting any url of the site with ?mod=yes will render the plugin template file, e.g.: http://example.com/hello-world/?mod=yes.
you need to create a folder '/woocommerce/' inside your plugin directory, inside woocommerce you need to add a folder say, for email 'emails' and put the required template inside '/emails/' to override. just copy paste this code in the main.php of your plugin.
<?php
/**
* Plugin Name: Custom Plugin
*/
function myplugin_plugin_path() {
// gets the absolute path to this plugin directory
return untrailingslashit( plugin_dir_path( __FILE__ ) );
}
add_filter( 'woocommerce_locate_template', 'myplugin_woocommerce_locate_template', 10, 3 );
function myplugin_woocommerce_locate_template( $template, $template_name, $template_path ) {
global $woocommerce;
$_template = $template;
if ( ! $template_path ) $template_path = $woocommerce->template_url;
$plugin_path = myplugin_plugin_path() . '/woocommerce/';
// Look within passed path within the theme - this is priority
$template = locate_template(
array(
$template_path . $template_name, $template_name
)
);
// Modification: Get the template from this plugin, if it exists
if ( ! $template && file_exists( $plugin_path . $template_name ) )
$template = $plugin_path . $template_name;
// Use default template
if ( ! $template )
$template = $_template;
// Return what we found
return $template;
}
?>
for reference template override using plugin

Fatal error: Call to a member function the_meta() wpalchemy

I'm having error when i call the_meta() from single.php, im using wordpress 3.4.1, and WordPress Alchemy Metabox Class 1.4.17, my functions.php
include_once ( get_template_directory() .'/metaboxes/setup.php');
include_once( get_template_directory() .'/metaboxes/custom-spec.php');
my setup.php
include_once WP_CONTENT_DIR . '/wpalchemy/MetaBox.php';
include_once WP_CONTENT_DIR . '/wpalchemy/MediaAccess.php';
// include css to help style our custom meta boxes
add_action( 'init', 'my_metabox_styles' );
function my_metabox_styles()
{
if ( is_admin() )
{
wp_enqueue_style( 'wpalchemy-metabox', get_stylesheet_directory_uri() . '/metaboxes/meta.css' );
}
}
$wpalchemy_media_access = new WPAlchemy_MediaAccess();
custom-spec.php
<?php
$custom_mb = new WPAlchemy_MetaBox(array
(
'id' => '_custom_meta',
'title' => 'My Custom Meta',
'template' => get_stylesheet_directory() . '/metaboxes/custom-meta.php',
));
and single.php
<?php //inside loop.......... ?>
<?php global $custom_mb ?>
<?php echo $custom_mb->the_meta();?>
ERROR is :Fatal error: Call to a member function the_meta() on a non-object in C:\wamp\www\wp\wp-content\themes\twentyeleven\single.php on line 19
pleas help me to find the cause of error
The main issue here is that the global $custom_mb is most probably nothing. So asking it to have a value is what is causing the error.
It does look like you have the correct code as per example here: http://www.farinspace.com/wpalchemy-metabox/
Use print_r($custom_mb); after the global to see what it holds. If it's nothing you know somethings wrong.
Also, the code you say is inside the loop should be just outside of it.
(Update) FIX: Add global $custom_mb; to the top of the new php page.

Obtaining wp_list_pages in a Wordpress widget

I am creating my own little widget sitemap to put in my footer. Here's my code:
function widget($args, $instance)
{
extract( $args );
$title = apply_filters('widget_title', $instance['title']);
echo $before_widget;
if ( $title ) { echo $before_title . $title . $after_title; }
// WIDGET CODE GOES HERE
// ?> THIS IS A TEST! <?php
?><ul><?php
wp_list_pages('title_li=<h2>MAP OF THE SITE</h2>&sort_column=menu_order&depth=0&include=57,55,59,61,63,65,192');
?></ul><?php
echo $after_widget;
}
but looks like wp_list_pages doesn't return anything. Not even the title. Indeed if I uncomment that "this is a test" then it's shown.
The strange thing is the same wp_list_page is implemented also in header.php to obtain menus.
First rule when creating Wordpress custom functions is to check if function with the same name already exists.
<?php if (!function_exists("function_name")) {...} ?>
Second rule is to name your function with really unique name, in order to avoid conflicts. In your case this function already exists, that's why you cannot extract the data as you expect...
One more thing, I assume you will put this whole function code in your theme's functions.php file.

Resources