Wordpress - Exclude page from Sidebar widget? - wordpress

I'm trying to hide a page from the 'Most Popular Post' widget in the sidebar.
I've got the 'WP Hide Post' plugin, It helps me hide from everywhere but that doesn't hide a page from the the widget.

Use the exclude argument in the WP_Query.
<?php
$excudeID = 30;// the post_id off the excluded page
$popularpost = new WP_Query( array( 'post__not_in' => array($excludeID) ) );
//use this query in your widget
while ( $popularpost->have_posts() ) : $popularpost->the_post();
the_title();
endwhile;
?>
Another option could be to write your own widget:
class drc_PopulairPostsWithoutThatone extends WP_Widget
{
public function __construct()
{
// Instantiate the parent object
parent::__construct(false, 'Populair Posts Title');
}
public function widget($args, $instance)
{
global $wpdb;
$excludeId = 30;
$return = "<ul>";
$query = $wpdb->prepare("SELECT * FROM wp_popularpostsdata, wp_posts WHERE wp_popularpostsdata.postid = $wpdb->posts.ID AND $wpdb->posts.post_type = 'page' AND wp_popularpostsdata.postid != %d ORDER BY pageviews DESC limit 10", $excludeId);
$tops = $wpdb->get_results($query);
foreach ($tops as $top) {
$return .= '<li>' . get_the_title($top->postid) . ' - ' . $top->pageviews . '</li>';
}
$return .= "</ul>";
return $return;
}
public function update($new_instance, $old_instance)
{
// Save widget options
}
public function form($instance)
{
// Output admin widget options form
}
}
function drc_register_widgets()
{
register_widget('drc_PopulairPostsWithoutThatone');
}
add_action('widgets_init', 'drc_register_widgets');
Query taken from: https://wordpress.org/support/topic/shortcode-get-the-least-popular-posts/
Documentation about widgets: https://codex.wordpress.org/Function_Reference/register_widget

Related

How to allow using shortcodes in post box

I think this code is responsible for don't let me use shortcodes in Peepso post box.
public function peepso_activity_remove_shortcode( $content )
{
foreach($this->shortcodes as $shortcode=>$class) {
foreach($this->shortcodes as $shortcode=>$class) {
$from = array('['.$shortcode.']','['.$shortcode);
$to = array('['.$shortcode.']', '['.$shortcode);
$content = str_ireplace($from, $to, $content);
}
}
return $content;
}

How to get option values in widget development

I am trying to learn WordPress' Widgets API and create my own widgets.
The problem: When I try to echo $instance['platform'], nothing appears.
I want to be able to get the value.
class Practice_Widget extends WP_Widget
{
function __construct()
{
$widget_ops = array(
'classname' => 'my-widget',
'description' => 'Second Widget'
);
parent::__construct('my_little_widget', 'Practice Second', $widget_ops);
}
public function widget($args, $instance)
{
echo $args['before_widget'];
echo $instance['platform'];
echo $args['after_widget'];
}
public function form ($instance)
{
?>
<p>
<label for = "<?php echo esc_attr($this->get_field_id('title')); ?>">
Title:
</label>
<select name="<?php echo esc_attr($this->get_field_name('platform')); ?>)">
<option value="face">Facebook</option>
<option value="insta">Instagram</option>
</select>
</p>
<?php
}
public function update ($new_instance, $old_instance)
{
$instance = array();
$instance['platform'] = ! empty($new_instance['platform']) ? $new_instance['platform'] : '';
}
}
Your update() function needs to return the $instance array, otherwise WordPress won't be able to save the new values (hence the reason why you don't see anything on the front-end):
public function update ($new_instance, $old_instance)
{
$instance = array();
$instance['platform'] = ! empty($new_instance['platform']) ? $new_instance['platform'] : '';
return $instance;
}
See the Widgets API documentation for more details.

show a message when updating a custom widget in wordpress backend

I'm developing a custom wordpress widget. The widget needs some data from the user and needs to check this data server-side.
I wrote the code that checks the data inside the function 'update' of the widget. When I press the button save of the widget the function update got called correctly and my validation is executed.
public function update( $new_instance, $old_instance ) {
$instance = array();
foreach ($this->fields as $field) {
$fieldName = $field['name'];
$instance[$fieldName] =
(!empty($new_instance[$fieldName]) strip_tags($new_instance[$fieldName]) : '' );
}
$check = validate($new_instance);
return $instance;
}
What I need is to display a message to the user based on the result of the validation. How can I do this? For what I've seen the function update is called through ajax so I can't use an admin notice.
Is it possible?How can I do that?
Try below code
add_action('admin_notices', 'misha_custom_order_status_notices');
function misha_custom_order_status_notices() {
global $pagenow, $typenow;
if( get_transient( 'fx-admin-notice-panel' )){
echo "<div class=\"updated\"><p>Custom notification comes here</p></div>";
}
}
public function update( $new_instance, $old_instance ) {
$instance = array();
foreach ($this->fields as $field) {
$fieldName = $field['name'];
$instance[$fieldName] =
(!empty($new_instance[$fieldName])?
strip_tags($new_instance[$fieldName]) :
''
);
}
$check = validate($new_instance);
set_transient( 'fx-admin-notice-panel', true, 5 );
return $instance;
}

Get multiple custom taxonomy and terms index using json-api

I use json-api plugin and I have three custom taxonomies. for example: car, phone, and book. I try to get all taxonomy index something like this
http://example.com/api/taxonomy/get_taxonomy_index/?taxonomy=car,phone,book
But it doesn't work for more than one taxonomy. Does Anyone know how to get all my custom taxonomy and terms index?
Here is JSON_API_Taxonomy_Controller:
class JSON_API_Taxonomy_Controller {
public function get_taxonomy_index() {
$terms = $this->get_terms();
return array(
'count' => count( $terms ),
'terms' => $terms
);
}
public function get_terms() {
global $json_api;
$taxonomy = $this->get_current_taxonomy();
if (!$taxonomy) {
$json_api->error("Not found.");
}
$wp_terms = get_terms( $taxonomy );
$terms = array();
foreach ( $wp_terms as $wp_term ) {
if ( $wp_term->term_id == 1 && $wp_term->slug == 'uncategorized' ) {
continue;
}
$terms[] = new JSON_API_Term( $wp_term );
}
return $terms;
}
protected function get_current_taxonomy() {
global $json_api;
$taxonomy = $json_api->query->get('taxonomy');
if ( $taxonomy ) {
return $taxonomy;
} else {
$json_api->error("Include 'taxonomy' var in your request.");
}
return null;
}
}
// Generic rewrite of JSON_API_Tag class to represent any term of any type of taxonomy in WP
class JSON_API_Term {
var $id; // Integer
var $slug; // String
var $title; // String
var $description; // String
function JSON_API_Term($term = null) {
if ($term) {
$this->import_wp_object($term);
}
}
function import_wp_object($term) {
$this->id = (int) $term->term_id;
$this->slug = $term->slug;
$this->title = $term->name;
$this->description = $term->description;
$this->post_count = (int) $term->count;
}
}
So I want json object output: registered taxonomies as parent and terms as child.

wordpress plugin shortcodes buffer

My developed wordpress plugin which is activated with a shortcode is breaking my admin area saying that header cannot be modified. Digging a bit deeper I got to know that if the function is getting echoed than I have this problem if I use return than is ok. But the problem with return is: that I use ajax to retrieve html and in this case no output is generated.
message Cannot modify header information - headers already sent by (output started at /var/www.... web/wordpress/wp-admin/admin-header.php
MyClass{
public function __construct()
public $data;
{
require_once(dirname(__FILE__) . '/class/class.another.php');
$this->data = new Another();
add_action( 'init', array( &$this, 'init' ) );
}
public function init()
{
add_shortcode( 'my_shortcode', array ($this, 'shortcode') );
if(isset($_POST['id'])){
$param = $this->data->output_ajax_html($_POST['id']);
echo $this->shortcode_html_extended($param);
//this part breaks the buffer without echo is working but the contertn won't show up
}
}
public function shortcode()
{
add_shortcode( 'my_shortcode', array ($this, 'shortcode_html') );
}
public function shortcode_html()
{
$html = "";
$html .="";
return $html;
}
public function shortcode_html_extended($param)
{
$html = "";
//mixed with php
$html .="";
return $html;
}
}
$test = new MyClass();

Resources