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

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.

Related

Render template wordpress plugin development

//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

Load CSS for User role subscriber (Wordpress)

I'm trying load a style sheet is a user role is subscriber, what am I doing wrong:
<?php
global $current_user;
get_currentuserinfo();
if ( user_can( $current_user, "subscriber" ) ){
echo '<link rel="stylesheet" href="/login/subscriber-style.css">'
}
?>
This is in the header.php of the theme
You're using an outdated method of getting the current user role. get_currentuserinfo() was deprecated in WordPress 4.5.
https://codex.wordpress.org/Function_Reference/get_currentuserinfo
Also, you shouldn't be loading CSS directly from your header.php file, you need to enqueue it instead.
Add the following code to your functions.php file.
function wpse_load_subscriber_stylesheet() {
if ( current_user_can( 'subscriber' ) ) {
wp_enqueue_style( 'login-subscriber-style', home_url( '/login/subscriber-style.css' ) );
}
}
add_action( 'wp_enqueue_scripts', 'wpse_load_subscriber_stylesheet' );
This assumes that your CSS is placed in your web root (https://example.com/login/subscriber-style.css). If it's in your theme folder instead then you need get_template_directory_uri() . '/login/subscriber-style.css'.
https://developer.wordpress.org/reference/functions/wp_enqueue_style/

Shortcode in wordpress post is not showing in proper place

I have generated these shortcodes.Whenever I am using these short codes in some particular place posts and pages are not showing in the right place.It comes at the beginning of the content.
Can anyone help me out here?
function __construct()
{
register_activation_hook( __FILE__, array($this,'my_auction_creator_activation' ));
add_action( 'add_meta_boxes', array($this,'my_auction_creator_custom_field' ));
add_action( 'save_post', array($this,'my_auction_creator_form_save' ));
add_shortcode('myauctioncreator_listing' , array($this,'ebay_listing' ));
add_shortcode('myauctioncreator_ads' , array($this,'ebay_ads' ));
add_shortcode('myauctioncreator_profile' , array($this,'ebay_profile' ));
add_shortcode('myauctioncreator_feedback' , array($this,'ebay_feedback' ));
}
To render the output of the shortcode, are you using print or echo?
If you are, you should be returning the content instead. Using print or echo will cause this issue.
i was getting exactly same issue as i was echo the contents
use like this
function shortcode_function()
{
ob_start();
//some echo or html code goes here
echo "<p>this is paragraph</p>";
return ob_get_clean();
}

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.

Working with creating wordpress post types

I'm creating my custom post type and all is working well however I just have a simple question concerning how I make it show up.
I've creative my custom post type in my fuctions.php file and made a single-speakers.php file. How do I direct my custom post type speakers page to my "SAFETY SPEAKERS" tab in the main navigation?
THIS IS MY SITE
This is the actual link to my custom post type page with my speakers on it
fuctions.php
add_action( 'init', 'create_my_post_types' );
function create_my_post_types() {
register_post_type( 'speakers',
array(
'labels' => array(
'name' => __( 'Speakers' ),
'singular_name' => __( 'Speakers' )
),
'public' => true,
)
);
}
single-speakers.php
<?php get_template_part( 'header', '2' ); // Header #2 (header-2.php) ?>
<?php query_posts( 'post_type=speakers'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div id="speakersBox" class="clearfix"><?php the_title('<h6>', '</h6>'); ?><br/><?php the_content(); ?></div>
<hr>
<?php endwhile; endif; ?>
<?php get_template_part( 'footer', '2' ); // Footer #2 (footer-2.php) ?>
Any help and or advice is appreciated!
Assuming you have single-speakers.php template to display Single Speaker Post type. Add template_redirect action by checking post. Put this in your functions.php file. i.e.
add_action('template_redirect', 'redirect_to_speaker');
function redirect_to_speaker(){
global $post;
if(is_single() && $post->post_type == 'speakers'){
include (TEMPLATEPATH . '/single-speakers.php');
exit;
}
}
Alternatively. You can use a filter called template_include. In fact use this one:
add_filter('template_include', 'my_speaker_template');
function my_speaker_template($template){
global $post;
if(is_single() && $post->post_type == 'speakers'){
$template = get_template_directory() . '/single-speakers.php';
}
return $template;
}
Also, read this article with some very good information about Templates: http://xazure.net/2011/06/tips-snippets/wordpress/changing-wordpress-template-with-template_include/
Okay, well I figured out a way. I created a new template page with the info from the single-speakers.php file (and called it page-speakers.php) and pointed my Safety Speakers page to that new page. It works great!
Thanks for your help Naveed, I appreciate it anyways.

Resources