Support » Allgemeine Fragen » Shortcode im custom Plugin

  • Hallo WP-Community,

    ich bin gerade dabei mir das erstellen von custom Plugins beizubringen. Anhand eines YouTube-Tutorials habe ich mir ein Plugin erstellt, welches den eigenen youtube channel als Widget ausgibt. Hierzu habe ich das Plugin pp-youtubesub genannt und folgende Dateien erstellt:

    – pp-youtubesub/pp-youtubesub.php
    – pp-youtubesub/includes/pp-youtubesub-class.php
    – pp-youtubesub/includes/pp-youtubesub-scripts.php
    – pp-youtubesub/css/
    – pp-youtubesub/js/

    Folgenden Inhalt haben diese Dateien:
    pp-youtubesub.php

    <?php
    /*
    Plugin Name: PontemPro Youtube Subscription
    Plugin URI: https://plugins.pontempro.com
    Description: Displays channel Youtube subscription.
    Version: 1.0.0
    Author: Kajen Jegatheesan
    Author URI: http://pontempro.com
    */
    
    //exit if file is accessed directly
    if(!defined('ABSPATH')){
      exit;
    }
    
    //load scripts
    require_once(plugin_dir_path(__FILE__).'/includes/pp-youtubesub-scripts.php');
    
    //load class
    require_once(plugin_dir_path(__FILE__).'/includes/pp-youtubesub-class.php');
    
    //load action
    //require_once(plugin_dir_path(__FILE__).'/includes/contact_action.php');
    
    //register widget
    function register_ppyoutubesub(){
      register_widget('pp_youtube_sub_Widget');
    }
    
    //hook widget to WordPress
    add_action('widgets_init', 'register_ppyoutubesub');
    

    pp-youtubesub-class.php

    <?php
    /**
     * Adds PontemPro Youtube Subscription widget.
     */
    class pp_youtube_sub_Widget extends WP_Widget {
    
    	/**
    	 * Register widget with WordPress.
    	 */
    	function __construct() {
    		parent::__construct(
    			'ppyoutubesub_widget', // Base ID
    			esc_html__( 'PontemPro Youtube Subscription', 'ppys_domain' ), // Name
    			array( 'description' => esc_html__( 'A widget, which display your Youtube channel subscription.', 'ppys_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']; //space for elements (e.g. <div>) before widget
    
    		if ( ! empty( $instance['title'] ) ) {
    			echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
    		}
    
    		//widget content output
    
    $instance['arrRpl1'] = array("\"","'","!","?","#","+",";",".","(",")","{","}","<",">","=","/","\\","ö","Ö","ä","Ä","ü","Ü","ß","¿","$","%","&","´","§","
    
    ");
    $instance['arrRpl2'] = array("\" ","' ","! ","? ","# ","+ ","; ",". ", "( ",") ","{ ","} ","< ","> ","= ","/ ","\\ ","ö ","Ö ","ä ","Ä ","ü ","Ü ","ß ","¿ ","$ ","% ","& ","´ ","§ ","");
    $instance['arrRpl'] = array_merge($instance['arrRpl1'] , $instance['arrRpl2'] );
    
    echo "<h1 style='display:none;'>PontemPro Youtube Subscription</h1>";
    
    		if (!empty($instance["cusID"])) {
    			echo "<!-- Youtube Subscription --> <div id='".str_replace($instance['arrRpl'],"",$instance['cusID'])." ppys-container ppys-container-".get_the_ID()."'>";
    		}
    		else {
    			echo "<!-- Youtube Subscription --> <div id='ppys-container ppys-container-".get_the_ID()."'>";
    		}
    
    		echo '<div class="g-ytsubscribe" data-'.$instance['channelID'].'="'.$instance['channelName'].'" data-layout="'.$instance['layout'].'" data-count="'.$instance['dcount'].'"></div>';
    echo "</div>";
    
    		echo $args['after_widget']; //space for elements (e.g. </div>) after widget
    	}
    
    	/**
    	 * Back-end widget form.
    	 *
    	 * @see WP_Widget::form()
    	 *
    	 * @param array $instance Previously saved values from database.
    	 */
    	public function form( $instance ) {
    			$title = ! empty( $instance['title'] ) ? $instance['title'] : esc_html__( '', 'ppys_domain' );
    			$channelID = ! empty( $instance['channelID'] ) ? $instance['channelID'] : esc_html__( 'Channel ID', 'ppys_domain' );
    			$channelName = ! empty( $instance['channelName'] ) ? $instance['channelName'] : esc_html__( 'Channel Name', 'ppys_domain' );
    			$layout = ! empty( $instance['layout'] ) ? $instance['layout'] : esc_html__( 'Layout', 'ppys_domain' );
    			$dcount = ! empty( $instance['dcount'] ) ? $instance['dcount'] : esc_html__( 'Dcount', 'ppys_domain' );
    			$cusID = ! empty( $instance['cusID'] ) ? $instance['cusID'] : esc_html__( '', 'ppys_domain' );
    		?>
    
    <!-- TITLE -->
    
    	<p>
    			<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>">
    				<?php esc_attr_e( 'Title:', 'ppys_domain' ); ?>
    			</label>
    
    			<input
    				class="widefat"
    				id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"
    				name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>"
    				type="text"
    				value="<?php echo esc_attr( $title ); ?>">
    				<span>Info: Leave blank if you don't want heading.</span>
    		</p>
    
    <!-- Source if Channel Name or ID -->
    		<p>
    			<label for="<?php echo esc_attr( $this->get_field_id( 'channelID' ) ); ?>">
    				<?php esc_attr_e( 'Channel Name / User or ID:', 'ppys_domain' ); ?>
    			</label>
    
    			<select
    				class="widefat"
    				id="<?php echo esc_attr( $this->get_field_id( 'channelID' ) ); ?>"
    				name="<?php echo esc_attr( $this->get_field_name( 'channelID' ) ); ?>">
    				<option value="channelid" <?php echo ($channelID == 'channelid') ? 'selected' : ''; ?>>
    					ID
    				</option>
    				<option value="channel" <?php echo ($channelID == 'channel') ? 'selected' : ''; ?>>
    					Name or User
    				</option>
    			</select>
    
    		</p>
    
    	<!-- Channel Name -->
    			<p>
    				<label for="<?php echo esc_attr( $this->get_field_id( 'channelName' ) ); ?>">
    					<?php esc_attr_e( 'Channel Name:', 'ppys_domain' ); ?>
    				</label>
    
    				<input
    					class="widefat"
    					id="<?php echo esc_attr( $this->get_field_id( 'channelName' ) ); ?>"
    					name="<?php echo esc_attr( $this->get_field_name( 'channelName' ) ); ?>"
    					type="text"
    					value="<?php echo esc_attr( $channelName ); ?>">
    			</p>
    
    			<!-- Layout -->
    					<p>
    						<label for="<?php echo esc_attr( $this->get_field_id( 'layout' ) ); ?>">
    							<?php esc_attr_e( 'Layout:', 'ppys_domain' ); ?>
    						</label>
    
    						<select
    							class="widefat"
    							id="<?php echo esc_attr( $this->get_field_id( 'layout' ) ); ?>"
    							name="<?php echo esc_attr( $this->get_field_name( 'layout' ) ); ?>">
    							<option value="default" <?php echo ($layout == 'default') ? 'selected' : ''; ?>>
    								Default
    							</option>
    							<option value="full" <?php echo ($layout == 'full') ? 'selected' : ''; ?>>
    								Full
    							</option>
    						</select>
    					</p>
    
    			<!-- Data Count -->
    					<p>
    						<label for="<?php echo esc_attr( $this->get_field_id( 'dcount' ) ); ?>">
    							<?php esc_attr_e( 'Subscriber count:', 'ppys_domain' ); ?>
    						</label>
    
    						<select
    							class="widefat"
    							id="<?php echo esc_attr( $this->get_field_id( 'dcount' ) ); ?>"
    							name="<?php echo esc_attr( $this->get_field_name( 'dcount' ) ); ?>">
    								<option value="default" <?php echo ($dcount == 'default') ? 'selected' : ''; ?>>
    										ON
    								</option>
    								<option value="hidden" <?php echo ($dcount == 'hidden') ? 'selected' : ''; ?>>
    									OFF
    								</option>
    							</select>
    					</p>
    
    					<!-- Custom DIV ID-->
    					<p>
    						<label for="<?php echo esc_attr( $this->get_field_id( 'cusID' ) ); ?>">
    							<?php esc_attr_e( 'Custom DIV-Container ID:', 'ppys_domain' ); ?>
    						</label>
    
    						<input
    							class="widefat"
    							id="<?php echo esc_attr( $this->get_field_id( 'cusID' ) ); ?>"
    							name="<?php echo esc_attr( $this->get_field_name( 'cusID' ) ); ?>"
    							type="text"
    							value="<?php echo esc_attr( $cusID ); ?>">
    							<span>(For multiple IDs, just enter each after space.)</span>
    
    								<?php  ?>
    
    						</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'] ) ) ? sanitize_text_field( $new_instance['title'] ) : '';
    		$instance['channelID'] = ( ! empty( $new_instance['channelID'] ) ) ? sanitize_text_field( $new_instance['channelID'] ) : '';
    		$instance['channelName'] = ( ! empty( $new_instance['channelName'] ) ) ? sanitize_text_field( $new_instance['channelName'] ) : '';
    		$instance['layout'] = ( ! empty( $new_instance['layout'] ) ) ? sanitize_text_field( $new_instance['layout'] ) : '';
    		$instance['dcount'] = ( ! empty( $new_instance['dcount'] ) ) ? sanitize_text_field( $new_instance['dcount'] ) : '';
    		$instance['cusID'] = ( ! empty( $new_instance['cusID'] ) ) ? sanitize_text_field( $new_instance['cusID'] ) : '';
    
    		return $instance;
    	}
    
    }

    pp-youtubesub-scripts.php

    <?php
    //add scripts
    function ppys_add_scripts(){
      //add main plugin CSS
      wp_enqueue_style('ppys-main-style', plugins_url(). '/pp-youtubesub/css/style.css');
      //add main JS
      wp_enqueue_script('ppys-main-script', plugins_url(). '/pp-youtubesub/js/main.js');
    }
    
    add_action('wp_enqueue_scripts', 'ppys_add_scripts');
    
    //add external scripts
    wp_enqueue_script('google-yts-script', 'https://apis.google.com/js/platform.js');

    Nun möchte ich für dieses Widget auch ein Shortcode erstellen. Hierfür habe ich einiges probiert wie zB.:

    // Add Shortcode
    function return_ppyoutubesub() {
      echo  instance[$pp_youtube_sub_Widget];
    }
    
    add_shortcode( 'ppys_short', 'return_ppyoutubesub' );

    Aber leider jedesmal ohne Erfolg. Könnt ihr mir helfen, wie ich das umsetzen kann? Vielen Dank im Voraus!

    BR

    • Dieses Thema wurde geändert vor 2 Jahren, 11 Monaten von the-dark-knight-22.
    • Dieses Thema wurde geändert vor 2 Jahren, 11 Monaten von the-dark-knight-22. Grund: Formattierung
    • Dieses Thema wurde geändert vor 2 Jahren, 11 Monaten von Bego Mario Garde. Grund: Codeformatierung
Ansicht von 2 Antworten - 1 bis 2 (von insgesamt 2)
  • Ich würde versuchen, die Ausgabe des Widgets in eine Funktion zu packen und diese Funktion dann über den Shortcode auszugeben.

    Denk bitte daran, dass die Callback-Funktion eines Shortcodes immer einen Inhalt mit return übergeben soll, damit der Inhalt beim Rendern des Beitrags/der Seite eingefügt wird. Der Beitrag/die Seite wird dann per echo ausgegeben.

    Grundsätzlich lassen sich Fragen zur Plugin-Programmierung leichter beantworten, wenn du dein Plugin als GitHub-Repo verfügbar machst – dann lässt sich das mit wenigen Mausklicks installieren und der Code kann auch gleich in einer lesbareren Form angeschaut werden.

    Allerdings sprengen Programmierfragen hier auch ein wenig den Rahmen eines Anwendersupports. WordPress Stackexchange ist da wahrscheinlich die bessere Anlaufstelle.

    Thread-Starter the-dark-knight-22

    (@the-dark-knight-22)

    Hi Bego Mario Garde,

    das habe ich ja hier gemacht:

    function return_ppyoutubesub() {
      echo  instance[$pp_youtube_sub_Widget];
    }

    Aber hat leider nicht funktioniert.

Ansicht von 2 Antworten - 1 bis 2 (von insgesamt 2)
  • Das Thema „Shortcode im custom Plugin“ ist für neue Antworten geschlossen.