タグ: WORDPRESS

  • Elementorで権限毎にwidgetを非表示にする方法

    Elementorで権限毎にwidgetを非表示にする方法

    // GET CURRENT USER ROLE
    function get_current_user_role() {
    if( is_user_logged_in() ) {
    $user = wp_get_current_user();
    $role = ( array ) $user->roles;
    return $role[0];
    } else {
    return false;
    }
    }

    // Remove the Elementor Widgets from the left-hand Panel in the Live Editor

    global $elementor_widget_blacklist;

    $elementor_widget_blacklist = [
    // FREE
    ‘read-more’,
    ‘sidebar’,
    ‘audio’,

    // PRO
    ‘posts’,
    ‘portfolio’,
    ‘slides’,
    // ‘form’,
    ‘login’,
    ‘media-carousel’,
    ‘testimonial-carousel’,
    ‘nav-menu’,
    ‘pricing’,
    ‘facebook-comment’,
    ‘nav-menu’,
    ‘animated-headline’,
    ‘price-list’,
    ‘price-table’,
    ‘facebook-button’,
    ‘facebook-comments’,
    ‘facebook-embed’,
    ‘facebook-page’,
    ‘add-to-cart’,
    ‘categories’,
    ‘elements’,
    ‘products’,
    ‘flip-box’,
    ‘carousel’,
    ‘countdown’,
    ‘share-buttons’,
    ‘author-box’,
    ‘breadcrumbs’,
    ‘search-form’,
    ‘post-navigation’,
    ‘post-comments’,
    ‘theme-elements’,
    // ‘blockquote’,
    ‘template’,
    ‘wp-widget-audio’,
    ‘woocommerce’,
    ‘social’,
    ‘library’,
    ‘reviews’,
    ‘gallery’,
    ‘table-of-contents’,
    ‘html’,
    ‘star-rating’,
    ‘sitemap’,
    ‘theme-site-logo’,
    ‘theme-site-title’,
    ‘theme-page-title’,
    ‘theme-post-title’,
    ‘theme-post-excerpt’,
    ‘post-info’,
    ‘theme-post-featured-image’
    ];

    // CHECK CURRENT USER ROLE AND EXECUTE RESTRICTION IF NEEDED (in thix exemple for editor role)

    if( is_admin() && get_current_user_role() == ‘editor’) {

    // Remove all the Elementor Free & Pro Widgets listed above
    add_action(‘elementor/widgets/widgets_registered’, function($widgets_manager){
    global $elementor_widget_blacklist;
    foreach($elementor_widget_blacklist as $widget_name){
    $widgets_manager->unregister_widget_type($widget_name);
    }
    }, 15);

    // Remove the regular WordPress Widgets from the left-hand Panel in the Live Editor
    if ( ! function_exists( ‘ddw_tweak_elementor_remove_wp_widgets’ ) ) :
    add_filter( ‘elementor/widgets/black_list’, ‘ddw_tweak_elementor_remove_wp_widgets’ );

    function ddw_tweak_elementor_remove_wp_widgets( $black_list ) {
    /** Bail early if Elementor not active or tweak not wanted */
    if ( ! defined( ‘ELEMENTOR_VERSION’ ) ) {
    return $black_list;
    }

    /**
    * Get all registered WordPress widgets, but only the classes
    * (= the first-level array keys)
    */
    $black_list = array_keys( $GLOBALS[ ‘wp_widget_factory’ ]->widgets );

    /** Return black list array for filter */
    return (array) $black_list;

    }
    endif;

    }

  • WordPressカテゴリーページで説明を表示する方法

    カテゴリーページで説明を読み込み出力方法をメモ。

    <?php if(!is_paged()):?>
    <?php if(category_description()):?>

    <?php echo category_description(); ?>

    <?php endif;?>

    一行目はカテゴリーページが数ページある場合に1ページ目にだけに出力るための条件分岐。

  • seoプラグインYoasto SEOのmeta部分にcustomfieldの値を使う方法

    seoプラグインYoasto SEOのmeta description部分は任意のcustomfield(カスタムフィールド)の値を使用する事ができる。

    %%cf_<custom-field-name>%%

    上記のタグで任意のカスタムフィールドを説明文に使うことが出来る。

  • WordPressでパーマリンクの「-2」を削除して数字だけのpermalinkにする

    wordpressはその使用上、数字だけのパーマリンクにしようとすると末尾に「-2」がついてしまう。

    これは年アーカイブで4桁数字を使用するためコンフリクトを避けているとのこと。

    パーマリンクの「-2」を削除して数字だけのpermalinkにする方法1

    プラグイン「 Custom Permalinks 」をインストールする方法

    パーマリンクの「-2」を削除して数字だけのpermalinkにする方法2

    mysqlで次のコマンドを実行し、「-2」を一括削除

    update wp_posts set post_name = replace(post_name, "-2", "") where post_type = "post";

     

  • wordpressのパフォーマンスを計測するプラグイン

    WordPress Plugin – Debug Bar
    WordPress Plugin – Debug-Bar-Extender

  • wordpressでメニュー構造だけをエクスポート(インポート)できるプラグイン

    メニュー構造が複雑多岐にわたる場合、他サイトへメニュー構造だけエクスポートしたいというケースがある。

    以下のプラグインで可能だ。

    WPS Menu Exporter

    インポートは普通にWordpressのインポート機能から。

  • kusanagiにインストーしたwordpressでウィジェットが更新できない

    kusanagiにインストーしたwordpressでウィジェットが更新できない場合、一度プラグインをすべてオフにして、一つづつ有効にして検証する。

    有効にした後はウィジェットページを一度読み込みなおしてから検証。

  • kusanagiにインストールしたWordPressの日付が英語になってしまう

    kusanagiにインストールしたWordpressの日付が英語になってしまう現象が起こったが以下で解決。

    ※具体的には年月のアーカイブが英語表記。

    kusanagiの設定から、翻訳アクセラレーターの有効のチェックボックスを外す。

  • wordpressで検索文字数、検索文字を制限する方法

    膨大な量のデータがある場合、検索でサーバーに負荷をかけないために、検索文字数や検索文字を制限したいということがある。

    検索文字数で条件分岐し制限する

    WordPressのフォームで検索文字数が少なかったり、多かったりするときにエラーメッセージを表示させる場合、以下のコードで条件分岐できる。

    <?php $s = $_GET['s'];?>
    <?php if (mb_strlen($s, 'UTF-8')<2) ://文字数が1文字?>

    上記の条件分岐を使えば一文字での検索を禁止することができる。

    検索文字で条件分岐し制限する

    <?php $s = $_GET['s'];?>
    <?php if (strcmp($s, '検索文字') == 0)) :?>

    上記の条件分岐を使えば、任意の文字列と比較し、一致すれば・・・・という処理を行うことができる。

     

  • WordPressで検索結果でパラメーターの値を保持させる方法

    WordPressの検索フォームをカスタマイズし、検索結果のページにもフォームを表示させている場合。

    検索クエリや絞り込み条件を保持させるておくと親切だ。

     

    検索結果でフォームの検索クエリを保持する方法

    inputに以下を追加

    value="<?php if(get_search_query()) echo get_search_query() ?>"

    検索結果でパラメータから値を取りだし保持する方法

    <?php function getParamVal($param) {
    $val
    = (isset($_GET[$param]) && $_GET[$param] != '') ? $_GET[$param] : '';
    $val
    = htmlspecialchars($val, ENT_QUOTES);

    return $val;
    } ?>

    inputに以下を追加

    value="<?php echo getParamVal('取得したいパラメータの値'); ?>"

    wp_dropdown_categoriesを使用し、パラメーターの値からカテゴリを保持

    <?php
    $selected_id = getParamVal('cat');
    $args = array(
    'show_option_all' => 'カテゴリー選択',
    'selected' => $selected_id,
    ); ?>
    <?php wp_dropdown_categories( $args ); ?> 

     

    タームの絞り込みでチェックボックスのcheckedの値を保持

    これが曲者だが、一つの方法はurlのパラメータで条件分岐する方法。

    urlにタームスラッグが含まれていれば、checkedでラベルを出力する。タームの種類が限られているならばこれで解決。

    <?php
    $url = $_SERVER['REQUEST_URI'];
    if(strstr($url,'タームスラッグ')==true):
    ?>
    <label><input type="checkbox" name="タクソノミー名[]" checked="checked" value="タームスラッグ">ターム名</label>
    <?php else: ?>
    <label><input type="checkbox" name="タクソノミー名[]" value="タームスラッグ">ターム名</label>
    <?php endif; ?>