So I am trying to make a WordPress widget that echoes out the movie input. I've put the echo in line 10 of the widget function however it doesn't echo out the input. Now I know I could swap this out to echo $instance['movie']; , however I want to test that my movie_search method works, which it isn't. Any help is appreciated and sorry if this answer is really stupid I am still quite new to coding.
<?php
public function widget( $args, $instance) {
echo $args['before_widget'];
if (!empty( $instance['tmdbapikey'] ) ) {
echo $args['before_tmdbapikey'].apply_filters(
'widget_tmdbapikey',
$instance['tmdbapikey']
).$args['after_tmdbapikey'];
}
//widget content output
echo $movie_query;
// whatever you want to display after widget (<div>, etc)
echo $args['after_widget'];
}
public function form( $instance ) {
$tmdbapikey = !empty( $instance['tmdbapikey'] )
? $instance['tmdbapikey']
: esc_html__( '', 'mpd_domain' );
<p>
<label for="<?php echo esc_attr($this->get_field_id('movie')); ?>">
<?php esc_attr_e('movie:', 'mpd_domain'); ?>
</label>
<input
class="widefat"
id="<?php echo esc_attr( $this->get_field_id( 'movie' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'movie' ) ); ?>"
type="text"
value="<?php echo esc_attr( $movie ); ?>">
</p>
This is the method for saving inputted user string.
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['tmdbapikey'] = (!empty($new_instance['tmdbapikey']))
? sanitize_text_field( $new_instance['tmdbapikey'] )
: '';
$instance['movie'] = (!empty($new_instance['movie']))
? sanitize_text_field($new_instance['movie'])
: '';
$instance['size'] = (!empty( $new_instance['size']))
? sanitize_text_field($new_instance['size'])
: '';
$instance['description'] = (!empty( $new_instance['description']))
? sanitize_text_field($new_instance['description'])
: '';
return $instance;
}
Movie search method
public function movie_search(){
//copies inputted data to movie variable
$movie_query = $instance['movie'];
}
Had to pass the $instance into the $movie_search method. So the line should have been public function movie_search($instance) as the variable was not a global variable like I originally thought it was, leaving it an empty variable. Also needed to add a return $movie_search to the last line of the movie search method.
Hence I had to change line 10 to echo $this->movie_search($instance).
Related
I was trying to make my metabox work but for some reason whenever I put some text and try to save the data inside the textarea it wont save.
add_action("admin_init", "custom_product_metabox");
function custom_product_metabox(){
add_meta_box("custom_product_metabox_01", "Product Description", "custom_product_metabox_field", "portfolio_page", "normal", "low");
}
function custom_product_metabox_field(){
global $page;
$data = get_post_custom($page->ID);
$val = isset($data['custom_product_input']) ? esc_attr($data['custom_product_input'][0]) : 'no value';
echo '<textarea rows="5" cols="220" name="custom_product_input" id="custom_product_input" value="'.$val.'"></textarea>';
}
add_action("save_post", "save_detail");
function save_detail(){
global $page;
if(define('DOING_AUTOSAVE') && DOING_AUTOSAVE){
return $page->ID;
}
update_post_meta($page->ID, "custom_product_input", $_POST["custom_product_input"]);
}
This is actually a code for a portfolio page where I embedded inside the functions.php. Any idea how can I make it work and save the data?
Thanks!
Your save method looks wrong. Try something like
function custom_product_metabox_field($post){
//global $page; remove this line also
$data = get_post_custom($post->ID);
$val = !empty(get_post_meta( $post->ID, 'custom_product_input', true )) ?
get_post_meta( $post->ID, 'custom_product_input', true ) : 'no value';
echo '<textarea rows="5" cols="220" name="custom_product_input" id="custom_product_input" value="'.$val.'"></textarea>';
}
add_action("save_post", "save_detail", 10, 3 );
function save_detail($post_id, $post, $update){
//global $page;// remove this line
if(define('DOING_AUTOSAVE') && DOING_AUTOSAVE){
return $post_id;
}
update_post_meta($post_id, "custom_product_input", $_POST["custom_product_input"]);
}
I'm using WP_Query to get posts from a custom post type to use the result in a metabox. Everything works great with my query. But after this query I can't get the other meta values from database.
This is my helper function to get custom field value:
function my_page_get_custom_field( $value ) {
global $post;
$custom_field = get_post_meta( $post->ID, $value, true );
if ( !empty( $custom_field ) )
return is_array( $custom_field ) ? stripslashes_deep( $custom_field ) : stripslashes( wp_kses_decode_entities( $custom_field ) );
return false;
}
Here's my query:
$sliderArgs = array(
'posts_per_page' => -1,
'post_type' => 'slider',
);
$slider = new WP_Query($sliderArgs);
if ($slider->have_posts()) {
?>
<select name="slider" id="slider">
$selectedSlide = my_page_get_custom_field('slider');
while($slider->have_posts()){
$slider->the_post();
$slideID = get_the_ID();
?><option value="<?php echo $slideID; ?>" <?php selected($selectedSlide, $slideID, true); ?>><?php the_title(); ?></option><?php
}
wp_reset_postdata(); ?>
</select>
}
And this is my other custom field which returns empty (there is a value in database and when I try to change it works great but not displaying in input value in admin):
<input type="text" name="meta_title" id="meta_title" value="<?php echo my_page_get_custom_field('meta_title'); ?>">
OK I solved it.
I used get_posts instead of WP_Query. This helped me a lot: https://core.trac.wordpress.org/ticket/18408#comment:5
This question already has answers here:
The plugin generated X characters of unexpected output during activation (WordPress)
(25 answers)
Closed 4 years ago.
I created a widget, but when i activate it, i get the following message:
"The plugin generated 3 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin."
code:
http://pastebin.com/MgV2ZmjJ
<?php
/*
Plugin Name: Kamera Widget
Description: kamera
*/
class KameraRa extends WP_Widget {
function __construct() {
parent::__construct(
'KameraRa',
__('Kamera', 'wpb_widget_domain'),
array( 'description' => __( 'Kamera v živo', 'wpb_widget_domain' ), )
);
}
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
echo $args['before_widget'];
if ( ! empty( $title ) )
echo $args['before_title'] . $title . $args['after_title'];
echo __( '<a target="_blank" href="http://www.ex.com">
<center><img width="100%" src="http://ex2.com" alt="RTC Kranjska Gora - Kekec"></center>
</a>', 'wpb_widget_domain' );
echo $args['after_widget'];
}
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
$title = __( 'Snežni park v živo', 'wpb_widget_domain' );
}
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>
<?php
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
}
function wpb_load_widget() {
register_widget( 'KameraRa' );
}
add_action( 'widgets_init', 'wpb_load_widget' );
?>
I tried removing all blank spaces around <?php and ?>, but it doesnt help.
What am i doing wrong?
The problem was UTF-8 encoding. I converted the files to ANSI and the problem is gone.
Found the answer here:
https://www.toddlahman.com/the-plugin-generated-x-characters-of-unexpected-output-during-activation/
1) Please remove extra spacing in the inner pages or plugin pages.
2) remove the inline styling in the pages or plugin pages.
its worked for me everytime. i have used many times same method. when the problem error in the plugin page or Wordpress page.
Regards,
Hitesh Nagpal
I'm trying to display a custom field in a widget I'm customizing but I can't get the custom field to display. I think it has something to do with the variable I'm using to get the post ID in the loop because when I change it to the standard the_title function, the widget works, so it has something to do with how I'm calling the custom field.
I know the key to the custom field is "wpcf-promo-title" but no matter what I've tried, the custom field (which holds a shortened version of the post title for the sidebar) just won't display. This code results in the thumbnails showing, but not the promo title. You can see this in action at http://www.cantstopshipping.com
Here's my code, including the query and the front end of the widget.
function widget($args, $instance) {
extract( $args );
$title = apply_filters( 'widget_title', empty($instance['title']) ? 'Recent Posts' : $instance['title'], $instance, $this->id_base);
$show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false;
if ( ! $number = absint( $instance['number'] ) ) $number = 5;
if( ! $cats = $instance["cats"] ) $cats='';
// array to call recent posts.
$crpw_args=array(
'showposts' => $number,
'category__in'=> $cats,
);
$crp_widget = null;
$crp_widget = new WP_Query($crpw_args);
echo $before_widget;
// Widget title
echo $before_title;
echo $instance["title"];
echo $after_title;
// Post list in widget
echo "<ul>\n";
while ( $crp_widget->have_posts() )
{
$crp_widget->the_post();
?>
<li class="crpw-item">
<p style="float:left">
<?php the_post_thumbnail('sidebar-small'); ?>
</p>
<?php $promotitle = get_post_meta($post->ID, 'wpcf-promo-title', true); ?>
<p style="float:right; width:200px">
<?php echo $promotitle; ?>
</p>
<?php if ( $show_date ) : ?>
<span class="crpw-date"><?php echo "("; ?><?php echo get_the_date(); ?><?php echo ")"; ?></span>
<?php endif; ?>
</li>
<?php
}
wp_reset_query();
echo "<div class=\"fix\"></div>";
echo "</ul>\n";
echo $after_widget;
}
It looks like your global $post is missing.
But you could try get_the_ID() instead of $post->ID.
You should also consider getting rid of extract(), it's now considered a "bad" practice.
Another thing is that you should use wp_reset_postdata() to restore the global $post object. The wp_reset_query() call should be used with the query_posts() call.
I'm trying to edit a WordPress front end user form plugin so that it will write away multiple check box values (from one of my custom fields) in a single SQL record. I need to do this so my query search form can search for all values checked off. Right now, if a user checks off more than one option, only one single value is being stored. How can I adjust this PHP code so that every value checked will be stored?
/**
* Prints a checkbox field
*
* #param array $attr
* #param int|null $post_id */
function checkbox( $attr, $post_id, $type ) {
$selected = isset( $attr['selected'] ) ? $attr['selected'] : array();
if ( $post_id ) {
$selected = explode( self::$separator, $this->get_meta( $post_id, $attr['name'], $type, true ) );
}
?>
<div class="wpuf-fields">
<span data-required="<?php echo $attr['required'] ?>" data-type="radio"></span>
<?php
if ( $attr['options'] && count( $attr['options'] ) > 0 ) {
foreach ($attr['options'] as $option) {
?>
<label>
<input type="checkbox" name="<?php echo $attr['name']; ?>[]" value="<?php echo esc_attr( $option ); ?>"<?php echo in_array( $option, $selected ) ? ' checked="checked"' : ''; ?> />
<?php echo $option; ?>
</label>
<?php
}
}
?>
Thanks in advance!