Skip to content

Instantly share code, notes, and snippets.

@bulentsakarya
Created September 26, 2017 18:18
Show Gist options
  • Save bulentsakarya/8f97c2a689e092d23d1976573b2e3b54 to your computer and use it in GitHub Desktop.
Save bulentsakarya/8f97c2a689e092d23d1976573b2e3b54 to your computer and use it in GitHub Desktop.
istediğiniz sayfaya özel sidebar oluşturma.
<?php
/**
* Nivoshop Custom Sidebar
*
* Özel sidebar metabox değeri true olan sayfaların sidebar'larının oluşturulduğu kod.
*
* @author Nivo Themes
* @since 1.0
* @package nivoshop
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class CustomSidebars {
public function __construct() {
add_action( 'widgets_init', array( $this, 'nivoshop_custom_widgets_init' ) );
}
public function nivoshop_custom_widgets_init() {
$sidebared_content = get_pages();
foreach ( $sidebared_content as $sp ) {
$page_id = $sp->ID;
$page_title = $sp->post_title;
$page_name = $sp->post_name;
$_showsidebar = get_post_meta($sp->ID, '_showsidebar', true);
if ( !empty($_showsidebar) && ( $_showsidebar == 'show' ) ) :
$sidebar_args['sidebar-'.$page_id] = array(
'name' => $page_title,
'id' => 'sidebar-'.$page_id,
'description' => $page_title . __(' sayfası bileşen alanı', 'nivoshop'),
);
$sidebar_args = apply_filters( 'nivoshop_sidebar_args', $sidebar_args );
foreach ( $sidebar_args as $sidebar => $args ) {
$widget_tags = array(
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
);
/**
* Dynamically generated filter hooks. Allow changing widget wrapper and title tags. See the list below.
*
* 'nivoshop_customsidebar_widget_tags'
*/
$filter_hook = sprintf( 'nivoshop_%s_widget_tags', $sidebar );
$widget_tags = apply_filters( $filter_hook, $widget_tags );
if ( is_array( $widget_tags ) ) {
register_sidebar( $args + $widget_tags );
}
}
endif;
}
wp_reset_postdata();
}
}
new CustomSidebars;
<?php
/**
* Contact Page Meta Box
*
* Sayfalarda sidebar oluşturup / oluşturma ve konumunu soracak metabox.
*
* @author Nivo Themes
* @since 1.0
* @package nivoshop
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class CustomSidebarMeta {
public function __construct() {
if ( is_admin() ) {
add_action( 'load-post.php', array( $this, 'init_metabox' ) );
add_action( 'load-post-new.php', array( $this, 'init_metabox' ) );
}
}
public function init_metabox() {
add_action( 'add_meta_boxes', array( $this, 'add_metabox' ), 10, 2 );
add_action( 'save_post', array( $this, 'save_metabox' ), 10, 2 );
}
public function add_metabox() {
add_meta_box(
'nivoshop-custom-sidebar',
__( 'Bileşen Alanı Yönetimi', 'nivoshop' ),
array( $this, 'nivo_customsidebar_metabox' ),
array( 'page', 'product' ),
'side',
'high'
);
}
public function nivo_customsidebar_metabox( $post ) {
// Add nonce for security and authentication.
wp_nonce_field( 'nivoshop_showsidebar_nonce_action', 'nivoshop_showsidebar_nonce' );
// Retrieve an existing value from the database.
$_showsidebar = get_post_meta( $post->ID, '_showsidebar', true );
$_sidebarposition = get_post_meta( $post->ID, '_sidebarposition', true );
// Set default values.
if( empty( $_showsidebar ) ) $_showsidebar = '';
// Form fields.
echo '<table class="form-table">';
echo ' <tr>';
echo ' <th style="padding-bottom: 0 !important;"><label for="_showsidebar" class="_showsidebar_label">' . __( 'İçeriğe Özel Bileşen Alanı', 'nivoshop' ) . '</label></th>';
echo ' </tr>';
echo ' <tr>';
echo ' <td style="padding-left:0">';
echo ' <label><input type="radio" name="_showsidebar" class="_showsidebar_field" value="show" ' . checked( $_showsidebar, 'show', false ) . '> ' . __( 'Etkinleştir', 'nivoshop' );
echo ' <input type="radio" name="_showsidebar" class="_showsidebar_field" value="hide" ' . checked( $_showsidebar, 'hide', false ) . '> ' . __( 'Etkinleştirme', 'nivoshop' );
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th style="padding-bottom: 0 !important;"><label for="_sidebarposition" class="_sidebarposition_label">' . __( 'Bileşen Alanı Konumu', 'nivoshop' ) . '</label></th>';
echo ' </tr>';
echo ' <tr>';
echo ' <td style="padding: 0 !important;">';
echo ' <select name="_sidebarposition">';
echo ' <option value="default" ' . selected( $_sidebarposition, 'left', false ) . '> ' . __( 'Genel Ayarları Kullan', 'nivoshop' ) . '</option>';
echo ' <option value="left" ' . selected( $_sidebarposition, 'left', false ) . '> ' . __( 'Bileşen Alanı Solda', 'nivoshop' ) . '</option>';
echo ' <option value="right" ' . selected( $_sidebarposition, 'right', false ) . '> ' . __( 'Bileşen Alanı Sağda', 'nivoshop' ) . '</option>';
echo ' </select>';
echo ' </td>';
echo ' </tr>';
echo '</table>';
}
public function save_metabox( $post_id, $post ) {
// Add nonce for security and authentication.
$nonce_name = isset( $_POST['nivoshop_showsidebar_nonce'] ) ? $_POST['nivoshop_showsidebar_nonce'] : '';
$nonce_action = 'nivoshop_showsidebar_nonce_action';
// Check if a nonce is set.
if ( ! isset( $nonce_name ) )
return;
// Check if a nonce is valid.
if ( ! wp_verify_nonce( $nonce_name, $nonce_action ) )
return;
// Check if the user has permissions to save data.
if ( ! current_user_can( 'edit_post', $post_id ) )
return;
// Check if it's not an autosave.
if ( wp_is_post_autosave( $post_id ) )
return;
// Check if it's not a revision.
if ( wp_is_post_revision( $post_id ) )
return;
// Sanitize input.
$_showsidebar_new = isset( $_POST[ '_showsidebar' ] ) ? sanitize_text_field( $_POST[ '_showsidebar' ] ) : '';
$_sidebarposition_new = isset( $_POST[ '_sidebarposition' ] ) ? sanitize_text_field( $_POST[ '_sidebarposition' ] ) : '';
// Update the meta field in the database.
update_post_meta( $post_id, '_showsidebar', $_showsidebar_new );
update_post_meta( $post_id, '_sidebarposition', $_sidebarposition_new );
}
}
new CustomSidebarMeta;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment