Skip to content

Instantly share code, notes, and snippets.

@pommiegranit
Last active August 30, 2018 12:54
Show Gist options
  • Select an option

  • Save pommiegranit/a942d291efdc5d60c030 to your computer and use it in GitHub Desktop.

Select an option

Save pommiegranit/a942d291efdc5d60c030 to your computer and use it in GitHub Desktop.

Revisions

  1. pommiegranit revised this gist Nov 13, 2014. 1 changed file with 52 additions and 0 deletions.
    52 changes: 52 additions & 0 deletions tpw_admin.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    var custom_uploader;

    function image_button_click(dialog_title, button_text, library_type, preview_id, control_id) {

    event.preventDefault();

    //If the uploader object has already been created, reopen the dialog
    //if (custom_uploader) {
    // custom_uploader.open();
    // return;
    //}

    //Extend the wp.media object
    custom_uploader = wp.media.frames.file_frame = wp.media({
    title: dialog_title,
    button: {
    text: button_text
    },
    library : { type : library_type },
    multiple: false
    });

    //When a file is selected, grab the URL and set it as the text field's value
    custom_uploader.on('select', function() {

    attachment = custom_uploader.state().get('selection').first().toJSON();
    jQuery('#' + control_id).val(attachment.url);

    var html = '';

    if (library_type=='image') {
    html = '<img src="' + attachment.url + '">';
    }

    if (library_type=='video') {
    html = '<video autoplay loop><source src="' + attachment.url + '" type="video/' + get_extension( attachment.url ) + '" /></video>';
    }

    jQuery('#' + preview_id).empty();
    jQuery('#' + preview_id).append(html);
    });

    //Open the uploader dialog
    custom_uploader.open();

    }

    function get_extension( url ){

    return url.substr((url.lastIndexOf('.') + 1));

    }
  2. pommiegranit created this gist Nov 13, 2014.
    93 changes: 93 additions & 0 deletions mywidget-basic.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,93 @@
    <?php
    /*
    Plugin Name: My Widget
    Plugin URI: http://mydomain.com
    Description: My first widget
    Author: Me
    Version: 1.0
    Author URI: http://mydomain.com
    */

    // Block direct requests
    if ( !defined('ABSPATH') )
    die('-1');


    add_action( 'widgets_init', function(){
    register_widget( 'My_Widget' );
    });

    /**
    * Adds My_Widget widget.
    */
    class My_Widget extends WP_Widget {

    /**
    * Register widget with WordPress.
    */
    function __construct() {
    parent::__construct(
    'My_Widget', // Base ID
    __('My Widget', 'text_domain'), // Name
    array( 'description' => __( 'My first widget!', 'text_domain' ), ) // Args
    );
    }

    /**
    * Front-end display of widget.
    *
    * @see WP_Widget::widget()
    *
    * @param array $args Widget arguments.
    * @param array $instance Saved values from database.
    */
    public function widget( $args, $instance ) {

    echo $args['before_widget'];
    if ( ! empty( $instance['title'] ) ) {
    echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ). $args['after_title'];
    }
    echo __( 'Hello, World!', 'text_domain' );
    echo $args['after_widget'];
    }

    /**
    * Back-end widget form.
    *
    * @see WP_Widget::form()
    *
    * @param array $instance Previously saved values from database.
    */
    public function form( $instance ) {
    if ( isset( $instance[ 'title' ] ) ) {
    $title = $instance[ 'title' ];
    }
    else {
    $title = __( 'New title', 'text_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
    }

    /**
    * Sanitize widget form values as they are saved.
    *
    * @see WP_Widget::update()
    *
    * @param array $new_instance Values just sent to be saved.
    * @param array $old_instance Previously saved values from database.
    *
    * @return array Updated safe values to be saved.
    */
    public function update( $new_instance, $old_instance ) {
    $instance = array();
    $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';

    return $instance;
    }

    } // class My_Widget
    159 changes: 159 additions & 0 deletions mywidget-useful.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,159 @@
    <?php
    /*
    Plugin Name: My Useful Widget
    Plugin URI: http://mydomain.com
    Description: My useful widget
    Author: Me
    Version: 1.0
    Author URI: http://mydomain.com
    */

    // Block direct requests
    if ( !defined('ABSPATH') )
    die('-1');


    add_action( 'widgets_init', function(){
    register_widget( 'My_Useful_Widget' );
    });

    /**
    * Adds My_Widget widget.
    */
    class My_Useful_Widget extends WP_Widget {

    /**
    * Register widget with WordPress.
    */
    function __construct() {
    parent::__construct(
    'My_Useful_Widget', // Base ID
    __('My Useful Widget', 'text_domain'), // Name
    array('description' => __( 'My useful widget!', 'text_domain' ),) // Args
    );
    }

    /**
    * Front-end display of widget.
    *
    * @see WP_Widget::widget()
    *
    * @param array $args Widget arguments.
    * @param array $instance Saved values from database.
    */
    public function widget( $args, $instance ) {

    // get the excerpt of the required story
    if ( $instance['story_id'] == 0 ) {

    $gp_args = array(
    'posts_per_page' => 1,
    'post_type' => 'story',
    'orderby' => 'post_date',
    'order' => 'desc',
    'post_status' => 'publish'
    );

    $posts = get_posts( $gp_args );

    if ( $posts ) {
    $post = $post[0];
    } else {
    $post = null;
    }

    } else {

    $post = get_post( $instance['story_id'] );

    }

    if ( array_key_exists('before_widget', $args) ) echo $args['before_widget'];

    if ( $post ) {

    echo get_the_post_thumbnail( $post->ID, array(250,500), array('class'=>'story_featured_img') );
    echo '<h3 class="story_widget_title">' . apply_filters( 'widget_title', $post->post_title ). '</h3>';
    echo '<p class="story_widget_excerpt">' . $post->post_excerpt . '</p>';
    echo '<p class="story_widget_readmore"><a href="' . get_permalink( $post->ID ) . '" title="Read the story, ' . $post->post_title . '">more...</a></p>';

    } else {

    echo __( 'No recent story found.', 'text_domain' );
    }

    if ( array_key_exists('after_widget', $args) ) echo $args['after_widget'];
    }

    /**
    * Back-end widget form.
    *
    * @see WP_Widget::form()
    *
    * @param array $instance Previously saved values from database.
    */
    public function form( $instance ) {

    if ( isset( $instance[ 'story_id' ] ) ) {
    $story_id = $instance[ 'story_id' ];
    }
    else {
    $story_id = 0;
    }
    ?>

    <p>
    <label for="<?php echo $this->get_field_id( 'story_id' ); ?>"><?php _e( 'Story:' ); ?></label>

    <select id="<?php echo $this->get_field_id( 'story_id' ); ?>" name="<?php echo $this->get_field_name( 'story_id' ); ?>">
    <option value="0">Most recent</option>
    <?php
    // get the exceprt of the most recent story
    $gp_args = array(
    'posts_per_page' => -1,
    'post_type' => 'story',
    'orderby' => 'post_date',
    'order' => 'desc',
    'post_status' => 'publish'
    );

    $posts = get_posts( $gp_args );

    foreach( $posts as $post ) {

    $selected = ( $post->ID == $story_id ) ? 'selected' : '';

    if ( strlen($post->post_title) > 30 ) {
    $title = substr($post->post_title, 0, 27) . '...';
    } else {
    $title = $post->post_title;
    }

    echo '<option value="' . $post->ID . '" ' . $selected . '>' . $title . '</option>';

    }

    ?>
    </select>
    </p>
    <?php
    }

    /**
    * Sanitize widget form values as they are saved.
    *
    * @see WP_Widget::update()
    *
    * @param array $new_instance Values just sent to be saved.
    * @param array $old_instance Previously saved values from database.
    *
    * @return array Updated safe values to be saved.
    */
    public function update( $new_instance, $old_instance ) {

    $instance = array();
    $instance['story_id'] = ( ! empty( $new_instance['story_id'] ) ) ? strip_tags( $new_instance['story_id'] ) : '';
    return $instance;
    }

    } // class My_Widget
    22 changes: 22 additions & 0 deletions tpw_construct.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    /**
    * Register widget with WordPress.
    */
    function __construct() {
    parent::__construct(
    'Title_Page_Widget', // Base ID
    __('Title Page Widget (PB)', 'text_domain'), // Name
    array('description' => __( 'Creates a full-screen title page - designed for use with Site Origin\'s Page Builder plugin', 'text_domain' ),) // Args
    );

    add_action( 'sidebar_admin_setup', array( $this, 'admin_setup' ) );

    }

    function admin_setup(){

    wp_enqueue_media();
    wp_register_script('tpw-admin-js', plugins_url('tpw_admin.js', __FILE__), array( 'jquery', 'media-upload', 'media-views' ) );
    wp_enqueue_script('tpw-admin-js');
    wp_enqueue_style('tpw-admin', plugins_url('tpw_admin.css', __FILE__) );

    }
    67 changes: 67 additions & 0 deletions tpw_form.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,67 @@
    /**
    * Back-end widget form.
    *
    * @see WP_Widget::form()
    *
    * @param array $instance Previously saved values from database.
    */
    public function form( $instance ) {


    $bg_video = ( isset( $instance['background_video'] ) ) ? $instance['background_video'] : '';
    $bg_image = ( isset( $instance['background_image'] ) ) ? $instance['background_image'] : '';
    $title_text = ( isset( $instance['title_text'] ) ) ? $instance['title_text'] : '';
    $title_image = ( isset( $instance['title_image'] ) ) ? $instance['title_image'] : '';


    ?>

    <div class="titlepage_widget">

    <h3>Title</h3>
    <p>
    <div class="widget_input">
    <label for="<?php echo $this->get_field_id( 'title_text' ); ?>"><?php _e( 'Text :' ); ?></label>
    <input class="title_text" id="<?php echo $this->get_field_id( 'title_text' ); ?>"
    name="<?php echo $this->get_field_name( 'title_text' ); ?>" value="<?php echo $title_text ?>" type="text"><br/>
    </div>
    <div class="widget_input">
    <label for="<?php echo $this->get_field_id( 'title_image' ); ?>"><?php _e( 'Image :' ); ?></label>
    <input class="title_image" id="<?php echo $this->get_field_id( 'title_image' ); ?>"
    name="<?php echo $this->get_field_name( 'title_image' ); ?>" value="<?php echo $title_image ?>" type="text">
    <button id="title_image_button" class="button"
    onclick="image_button_click('Choose Title Image','Select Image','image','title_image_preview','<?php echo $this->get_field_id( 'title_image' ); ?>');">Select Image</button>
    </div>
    <div id="title_image_preview" class="preview_placholder">
    <?php
    if ($title_image!='') echo '<img src="' . $title_image . '">';
    ?>
    </div>
    </p>

    <h3>Background</h3>
    <p id="title_background_inputs">
    <label for="<?php echo $this->get_field_id( 'background_video' ); ?>"><?php _e( 'Video :' ); ?></label>
    <input class="background_video" id="<?php echo $this->get_field_id( 'background_video' ); ?>"
    name="<?php echo $this->get_field_name( 'background_video' ); ?>" value="<?php echo $bg_video ?>" type="text">
    <button id="background_video_button" class="button" onclick="image_button_click('Choose Background Video','Select Video','video','background_video_preview','<?php echo $this->get_field_id( 'background_video' ); ?>');">Select Video</button>
    <div id="background_video_preview" class="preview_placholder">
    <?php
    if ($bg_video!='') echo '<video autoplay loop><source src="' . $bg_video . '" type="video/' . substr( $bg_video, strrpos( $bg_video, '.') + 1 ) . '" /></video>';
    ?>
    </div>
    <label for="<?php echo $this->get_field_id( 'background_image' ); ?>"><?php _e( 'Image :' ); ?></label>
    <input class="background_image" id="<?php echo $this->get_field_id( 'background_image' ); ?>"
    name="<?php echo $this->get_field_name( 'background_image' ); ?>" value="<?php echo $bg_image ?>" type="text">
    <button id="background_image_button" class="button" onclick="image_button_click('Choose Background Image','Select Image','image','background_image_preview','<?php echo $this->get_field_id( 'background_image' ); ?>');">Select Image</button>
    <div id="background_image_preview" class="preview_placholder">
    <?php
    if ($bg_image!='') echo '<img src="' . $bg_image . '">';
    ?>
    </div>
    </p>

    </div>

    <?php
    }
    58 changes: 58 additions & 0 deletions tpw_template.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    <?php
    /*
    * Template for the output of the Title Page Widget
    * Override by placing a file called tpw_template.php in your active theme
    */

    /* Output
    background image
    background video
    title text
    title image
    */
    $upload_dir = wp_upload_dir();

    echo '<!-- full-screen title page -->
    <header>';

    // background video

    echo '<!-- background video -->
    <div class="bgvideo-wrapper">';

    if ( isset( $instance['background_video'] ) ) {

    $video_dir = str_replace( $upload_dir['baseurl'], $upload_dir['basedir'], $instance['background_video'] );

    echo '<video class="bgvideo" preload="auto" loop autoplay poster="' . $instance['background_image'] . '">';

    $ext = '.' . wp_check_filetype( $video_dir )['ext'];

    // check for mp4 in same folder and output if found
    $new_video_dir = str_replace( $ext, '.mp4', $video_dir );
    if ( file_exists( $new_video_dir ) ) echo '<source src="' . str_replace( $ext, '.mp4', $instance['background_video'] ) . '" type="video/mp4">';

    // check for webm in same folder and output if found
    $new_video_dir = str_replace( $ext, '.webm', $video_dir );
    if ( file_exists( $new_video_dir ) ) echo '<source src="' . str_replace( $ext, '.webm', $instance['background_video'] ) . '" type="video/webm">';

    // check for ogg in same folder and output if found
    $new_video_dir = str_replace( $ext, '.ogg', $video_dir );
    if ( file_exists( $new_video_dir ) ) echo '<source src="' . str_replace( $ext, '.ogg', $instance['background_video'] ) . '" type="video/ogg">';

    echo '</video>';
    }

    echo '</div>
    <h1 class="maintitle">';

    if ( isset( $instance['title_image'] ) ) {
    echo '<img src="' . $instance['title_image'] . '">';
    } elseif ( isset( $instance['title_text'] ) ) {
    echo $instance['title_text'];
    }

    echo '</h1>';
    '</header>';
    ?>
    19 changes: 19 additions & 0 deletions tpw_update.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    /**
    * Sanitize widget form values as they are saved.
    *
    * @see WP_Widget::update()
    *
    * @param array $new_instance Values just sent to be saved.
    * @param array $old_instance Previously saved values from database.
    *
    * @return array Updated safe values to be saved.
    */
    public function update( $new_instance, $old_instance ) {

    $instance = array();
    $instance['background_video'] = ( ! empty( $new_instance['background_video'] ) ) ? strip_tags( $new_instance['background_video'] ) : '';
    $instance['background_image'] = ( ! empty( $new_instance['background_image'] ) ) ? strip_tags( $new_instance['background_image'] ) : '';
    $instance['title_text'] = ( ! empty( $new_instance['title_text'] ) ) ? strip_tags( $new_instance['title_text'] ) : '';
    $instance['title_image'] = ( ! empty( $new_instance['title_image'] ) ) ? strip_tags( $new_instance['title_image'] ) : '';
    return $instance;
    }
    21 changes: 21 additions & 0 deletions tpw_widget.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    /**
    * Front-end display of widget.
    *
    * @see WP_Widget::widget()
    * the
    * @param array $args Widget arguments.
    * @param array $instance Saved values from database.
    */
    public function widget( $args, $instance ) {

    // use a template for the output so that it can easily be overridden by theme

    // check for template in active theme
    $template = locate_template(array('tpw_template.php'));

    // if none found use the default template
    if ( $template == '' ) $template = 'tpw_template.php';

    include ( $template );

    }