I have 4 menus registered within my WordPress theme.
register_nav_menus( array(
'menu-1' => esc_html__( 'Primary', 'honeycomb' ),
'menu-2' => esc_html__( 'Mobile Cat Products', 'honeycomb' ),
'menu-3' => esc_html__( 'Top Bar', 'honeycomb' ),
'menu-4' => esc_html__( 'Shopify Nav', 'honeycomb' ),
) );
I need to dynamically insert a new menu link at the beginning of menu-4 From what I understand its possible to filter a specific menu using a variant of wp_nav_menu_items by simply passing in the theme location name of the menu. In my case 'menu-4'.
add_filter( 'wp_nav_menu_menu-4_items', 'prefix_add_menu_item', 10, 2 );
function prefix_add_menu_item ( $items, $args ) {
$items .= '<li class="menu-item">My New Item Here</li>';
return $items;
}
However this is not working. I am not sure what I am missing here.
The $menu->slug here apply_filters( "wp_nav_menu_{$menu->slug}_items", $items, $args ); is not the nav menu id... it's the Menu Name slug... like here i have named my Menu as Main Menu so the slug becomes main-menu so your filter would be...
add_filter( 'wp_nav_menu_main-menu_items', 'so68759013_add_menu_item', 10, 2 );
function so68759013_add_menu_item( $items, $args )
{
$items .= '<li class="menu-item">My New Item Here</li>';
return $items;
}
I have a small problem
I wrote a simple shortcode function to display my acf value in the grid of the visual grid composer, this is my simple custom shortcode
function my_module_add_grid_shortcodes( $shortcodes ) {
$shortcodes['vc_say_hello'] = array(
'name' => __( 'Say Hello', 'my-text-domain' ),
'base' => 'vc_say_hello',
'category' => __( 'Content', 'my-text-domain' ),
'description' => __( 'Just outputs Hello World', 'my-text-domain' ),
'post_type' => Vc_Grid_Item_Editor::postType(),
);
return $shortcodes;
}
add_shortcode( 'vc_say_hello', 'vc_say_hello_render' );
function vc_say_hello_render() {
if( get_field('item') ):
the_field('item');
else:
echo "<h2>ITEM LOCKED</h2>";
endif;
}
When I call the shortcode in the builder, "ITEM Locked" is displayed, even if the value of the element is set in the post !!!
Looks like get_field doesn't know where to get it from in the shortcode. Try adding the current post id as the second parameter
add_shortcode( 'vc_say_hello', 'vc_say_hello_render' );
function vc_say_hello_render() {
$id = get_the_ID();
if( get_field('item', $id) ):
the_field('item', $id);
else:
echo "<h2>ITEM LOCKED</h2>";
endif;
}
In functions.php I added this code.
function page_widget_init( $post_id = 0 ) { {
register_sidebar( array(
'name' => __( 'Page Widgets', 'seven_theme' ),
'id' => 'page-widgets-' . $post_id,
'description' => __( 'To display Layers Widgets in front page, before your posts, after header.', 'seven_theme' ),
) );
}
add_action( 'widgets_init', 'page_widget_init', 100 );
First how to get post id in functions.php register_sidebar $post_id
Now going to page template which i created using page.php, called Blog Template
Call this widget in this page.
dynamic_sidebar( 'page-widgets-' . $post->ID );
My idea is to create unique widget content using post ID.
I'm creating a plugin for a custom post type. I want to add a custom template for it. But I'm not sure how to add it via the plugin.
How can I add a custom post type template via the plugin?
Please help!
You can simply create and assign custom page templates for your custom post type in your custom plugin.
Just create 2 template file - single-{post_type}.php and archive-{post_type}.php - in a new templates sub-directory of your plugin directory.
Then add some code as per below example in your main plugin:
/*
* Set Page templates for CPT "your_cpt"
*/
add_filter( 'template_include', 'my_plugin_templates' );
function my_plugin_templates( $template ) {
$post_type = 'your_cpt'; // Change this to the name of your custom post type!
if ( is_post_type_archive( $post_type ) && file_exists( plugin_dir_path(__DIR__) . "templates/archive-$post_type.php" ) ){
$template = plugin_dir_path(__DIR__) . "templates/archive-$post_type.php";
}
if ( is_singular( $post_type ) && file_exists( plugin_dir_path(__DIR__) . "templates/single-$post_type.php" ) ){
$template = plugin_dir_path(__DIR__) . "templates/single-$post_type.php";
}
return $template;
}
Hope this example would be helpful for you.
Cheers !!
Below is a complete (blank) plugin that I put together based on my previously posted answer. I loaded it in my theme (based on twentyfifteen) and it works. Together with the plugin, as-is, you would also need The following file structure
- schs-blank/css schs-blank/js schs-blank/images
schs-blank/archive-blank.php schs-blank/single-blank.php
schs-blank/schs-blank-edit-posts.php
schs-blank/schs-blank-template.php
It should not be too tricky to figure out what should be in the above files, but for starters just put "<h1>{filename}</h1>" to see where each page is called.
<?php
/*
Plugin Name: SCHS Blank Plugin
Plugin URI: http://www.southcoasthosting.com/schs-blank
Tags: jquery, flyout, vertical, menu, animated, css, navigation, widget, plugin, scroll
Description: Creates a widget to place a vertical menu which caters for many menu items.
Author: Gavin Simpson
Version: 0.1
Author URI: http://www.southcoasthosting.com
074 355 1881
*/
class schs_blank
{
protected $plugin_slug;
private static $instance;
protected $templates;
public static function get_instance()
{
if( null == self::$instance )
{
self::$instance = new schs_blank();
}
return self::$instance;
}
private function __construct()
{
$this->templates = array();
$page_template = dirname( __FILE__ ) . '/schs-blank-template.php';
$this->templates = array('schs-blank-template.php' => 'SCHS Blank Page Template','schs-blank-edit-posts.php'=>'Edit Blank Post Template');
if(!is_admin())
{
add_action( 'wp_blank', array('schs_blank', 'header') );
}
else
add_action( 'wp_blank', array('schs_blank', 'footer') );
add_filter('page_attributes_dropdown_pages_args',array( $this, 'register_project_templates' ));
add_filter('wp_insert_post_data', array( $this, 'register_project_templates' ));
add_filter( 'template_include', array($this,'schs_force_template' ));
add_filter('template_include', array( $this, 'view_project_template') );
add_action('admin_menu', array($this,'setup_blank_admin_menus'));
add_action( 'init', array($this,'blank_custom_post' ));
}
function schs_force_template($template)
{
if( is_archive( 'blank' ) )
{
$template = WP_PLUGIN_DIR .'/'. plugin_basename( dirname(__FILE__) ) .'/archive-blank.php';
}
if( is_singular( 'blank' ) )
{
$template = WP_PLUGIN_DIR .'/'. plugin_basename( dirname(__FILE__) ) .'/single-blank.php';
}
return $template;
}
function header(){
wp_enqueue_script('jquery');
wp_enqueue_style( 'blank-css', schs_blank::get_plugin_directory() . "/css/blank.css" );
wp_enqueue_script('schs_blank', schs_blank::get_plugin_directory() . '/js/schs-blank.js', array('jquery'));
}
function footer()
{
}
function get_plugin_directory(){
return WP_PLUGIN_URL . '/schs-blank';
}
function setup_blank_admin_menus()
{
add_menu_page('SCHS Blank Settings', 'SCHS Blank Settings', 'manage_options',
'SCHS_blank_settings', array($this,'SCHS_page_settings'));
add_submenu_page('SCHS_blank_settings',
'SCHS Page Elements', 'Blank Topics', 'manage_options',
'SCHS_blank_topics_settings', array($this,'SCHS_blank_topics_settings'));
}
function SCHS_page_settings()
{
?>
<div class="wrap">
<h2>There are no Blank options at this stage.</h2>
</div>
<?php
}
function SCHS_blank_topics_settings()
{
?>
<div id="blank_topics" class="wrap">
<h1>There are no blank topics at this stage</h1>
</div>
<?php
}
function blank_custom_post()
{
$labels = array(
'name' => _x( 'Blank', 'post type general name' ),
'singular_name' => _x( 'Topic', 'post type singular name' ),
'add_new' => _x( 'Add New', 'book' ),
'add_new_item' => __( 'Add New Blank Topic' ),
'edit_item' => __( 'Edit Blank Topic' ),
'new_item' => __( 'New Blank Topic' ),
'all_items' => __( 'All Blank Topics' ),
'view_item' => __( 'View Blank Topics' ),
'search_items' => __( 'Search Blank Topics' ),
'not_found' => __( 'No blank topics found' ),
'not_found_in_trash' => __( 'No blank topics found in the trash' ),
'parent_item_colon' => '',
'menu_name' => 'Blank'
);
$args = array(
'labels' => $labels,
'description' => 'Holds our blank topic specific data',
'public' => true,
'menu_position' => 5,
'supports' => array( 'title', 'editor'),
'has_archive' => true,
'menu_icon' => $this->get_plugin_directory().'/images/blank-icon.png',
);
register_post_type( 'blank', $args );
flush_rewrite_rules();
}
public function register_project_templates( $atts )
{
// Create the key used for the themes cache
$cache_key = 'page_templates-' . md5( get_theme_root() . '/' . get_stylesheet() );
// Retrieve the cache list.
// If it doesn't exist, or it's empty prepare an array
$templates = wp_get_theme()->get_page_templates();
if ( empty( $templates ) ) {
$templates = array();
}
// New cache, therefore remove the old one
wp_cache_delete( $cache_key , 'themes');
// Now add our template to the list of templates by merging our templates
// with the existing templates array from the cache.
$templates = array_merge( $templates, $this->templates );
// Add the modified cache to allow WordPress to pick it up for listing
// available templates
wp_cache_add( $cache_key, $templates, 'themes', 1800 );
return $atts;
}
/**
* Checks if the template is assigned to the page
*/
public function view_project_template( $template )
{
global $post;
if (!isset($this->templates[get_post_meta(
$post->ID, '_wp_page_template', true
)] ) ) {
return $template;
}
$file = plugin_dir_path(__FILE__). get_post_meta(
$post->ID, '_wp_page_template', true
);
// Just to be safe, we check if the file exist first
if( file_exists( $file ) )
{
return $file;
}
else
{
echo $file;
exit;
}
return $template;
}
}
add_action( 'plugins_loaded', array( 'schs_blank', 'get_instance' ) );
?>
I hope this helps a bit more.
This work for me, kindly try it, Thanks
Templates is loaded into cpt file, which was located at
custom_plugin -> app -> cpt -> cpt_article.php
Template is located
custom_plugin -> app -> templates
add_filter( 'single_template', 'load_my_custom_template', 99, 1 );
function load_custom_templates($single_template) {
global $post;
if ($post->post_type == 'article' ) {
$single_template = trailingslashit( plugin_dir_path( __FILE__ ) .'app/templates' ).'single_article.php';
}
return $single_template;
}
You create a template file in your plugin, e.g /templates/myposttype-page.php
Then add below code into your plugin.
Change 'myposttype' to your post type
$custom_post_type = 'myposttype';
add_filter("theme_{$custom_post_type}_templates", "add_{$custom_post_type}_template");
add_filter('single_template', "redirect_{$custom_post_type}_template");
function add_myposttype_template() {
$templates['myposttype-page.php'] = 'My type Template';
return $templates;
}
function redirect_myposttype_template ($template) {
if( is_page_template('myposttype-page.php') ){
$template = plugin_dir_path(__FILE__). 'templates/myposttype-page.php';
}
return $template;
}
I have added some custom fields in woocommerce when adding product. Now i want to display those custom fields in product listing page ( wp-admin/edit.php?post_type=product ). I also want to quick edit and save like other values can be edit and save in listing page.
I tried bellow code but it did not work.
add_filter( 'manage_edit-product', 'show_product_order' );
function show_product_order($columns){
$new_columns = (is_array($columns)) ? $columns : array();
unset( $new_columns['order_actions'] );
$new_columns['product_order'] = 'Product Order';
$new_columns['order_actions'] = $columns['order_actions'];
return $new_columns;
}
I also tried below code but it did not worked too.
add_action( 'woocommerce_product_options_general_product_data', 'woocommerce_general_product_data_custom_field' );
function woocommerce_general_product_data_custom_field() {
global $woocommerce, $post;
echo '<div class="options_group">';
woocommerce_wp_checkbox(
array(
'id' => 'product_order',
'wrapper_class' => 'checkbox_class',
'label' => __('Order for Product', 'woocommerce' ),
'description' => __( 'Order for Product', 'woocommerce' )
)
);
echo '</div>';
}
i also reffered this post WooCommerce show custom column but i did not succeed to get solution
Please help.