How to hard code widget from this code in wordpress?

There is a widget on my site that I can add using the Widgets area. However, I need to hard-code it because if I add it through Widgets it appears on like 30 different pages. At that point I can only go to all those 30 pages and add display:none to hide it from each page individually. That would suck, and it would be bad for the SEO of my site…

I know the php file where the widget is called from. Here is the code of that php file:

Read More

[code]

<?php

add_action('widgets_init', 'register_featured_jobs_widget');
function register_featured_jobs_widget() {
    register_widget('PricerrTheme_featured_jobs');
}

class PricerrTheme_featured_jobs extends WP_Widget {

    function PricerrTheme_featured_jobs() {
        $widget_ops = array( 'classname' => 'featured-jobs', 'description' => 'Show latest featured jobs' );
        $control_ops = array( 'width' => 200, 'height' => 250, 'id_base' => 'featured-jobs' );
        $this->WP_Widget( 'featured-jobs', 'PricerrTheme - Featured Jobs', $widget_ops, $control_ops );
    }

    function widget($args, $instance) {
        extract($args);

        echo $before_widget;

        if ($instance['title']) echo $before_title . apply_filters('widget_title', $instance['title']) . $after_title;
        $limit = $instance['show_jobs_limit'];

        if(empty($limit) || !is_numeric($limit)) $limit = 5;

                 global $wpdb;  
                 $querystr = "
                    SELECT distinct wposts.* 
                    FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta, $wpdb->postmeta wpostmeta2 , $wpdb->postmeta wpostmeta4
                    WHERE wposts.ID = wpostmeta.post_id AND wposts.ID = wpostmeta2.post_id AND wposts.ID = wpostmeta4.post_id
                    AND wpostmeta.meta_key = 'closed' 
                    AND wpostmeta.meta_value = '0' 

                    AND wpostmeta4.meta_key = 'active' 
                    AND wpostmeta4.meta_value = '1' 

                    AND wpostmeta2.meta_key = 'featured' 
                    AND wpostmeta2.meta_value = '1' AND 

                    wposts.post_status = 'publish' 
                    AND wposts.post_type = 'job' 
                    ORDER BY wposts.post_date DESC LIMIT ".$limit;

                 $pageposts = $wpdb->get_results($querystr, OBJECT);

                 ?>

                     <?php $i = 0; if ($pageposts): ?>
                     <?php global $post; ?>
                     <?php foreach ($pageposts as $post): ?>
                     <?php setup_postdata($post); ?>


                     <?php PricerrTheme_small_post(); ?>


                     <?php endforeach; ?>
                     <?php else : ?> <?php $no_p = 1; ?>
                       <div class="padd100"><p class="center"><?php _e("Sorry, there are no posted jobs yet.",'PricerrTheme'); ?></p></div>

                     <?php endif; ?>

                     <?php

        echo $after_widget;
    }

    function update($new_instance, $old_instance) {

        return $new_instance;
    }

    function form($instance) { ?>
        <p>
            <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title'); ?>:</label>
            <input type="text" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" 
            value="<?php echo esc_attr( $instance['title'] ); ?>" style="width:95%;" />
        </p>


        <p>
            <label for="<?php echo $this->get_field_id('show_jobs_limit'); ?>"><?php _e('Show jobs limit'); ?>:</label>
            <input type="text" id="<?php echo $this->get_field_id('show_jobs_limit'); ?>" name="<?php echo $this->get_field_name('show_jobs_limit'); ?>" 
            value="<?php echo esc_attr( $instance['show_jobs_limit'] ); ?>" style="width:20%;" />
        </p>




    <?php 
    }
}



?>

[code]

I tried copying various parts from the code and putting them where I need to put the widget, but I can’t get it to work. Everything I tried either shows the actual code on my site, or breaks the page.

Can anyone help? Thank you.

Related posts

1 comment

  1. Assuming that your page slug is "my_page", then you can do this at the beginning of your code:

    <?php
    
    add_action('widgets_init', 'register_featured_jobs_widget');
    function register_featured_jobs_widget() {
    
        if ( is_page( "my_page" ) ){
           register_widget('PricerrTheme_featured_jobs');
        }
    
    }
    

    This will register your widget only on that particular page.

    Documentation from wordpress: is_page()

Comments are closed.