wordpress vurgulama eklentisi

Genel Forum
Cevapla
muratca61
Site Admin
Mesajlar: 35964
Kayıt: Cmt Ara 21, 2024 7:56 am

wordpress vurgulama eklentisi

Mesaj gönderen muratca61 »

chatgpt sorduk

Kod:Tümünü seç

wordpress için bir shortcode yapalım. [vurgulama] bu kod sadece eklendiği yazının 
 içeriğinde okuma kolaylığı için değişiklikler yapacak. bu ayarlar admin panelinde kullanıcı tarafından seçenklendirilebilir. 
-örneğin ilk paragrafı kalın metin yapabilir
-Büyük harfli kelimeler vurgulanabilir (renkli , kalın , highlight vb seçenekli)
-tırnak içerisindeki vurgulanabilir (kalın , renkli vb seçenekli)
- sayısal değerler vurgulanabilir (kalın vb seçenekli)
bunlar yazı okuma kolaylığı en iyi hale gelebilecek şekilde çeşitlendirip admin panelinde ayarları 
kullanıcının seçeneği ile ayar yapabilelim. uygulamanın aktif pasif tuşu olsun 
muratca61
Site Admin
Mesajlar: 35964
Kayıt: Cmt Ara 21, 2024 7:56 am

Re: wordpress vurgulama eklentisi

Mesaj gönderen muratca61 »

Aşağıdaki örnek kod, WordPress’te bir eklenti olarak kullanabileceğiniz, [vurgulama] shortcode’unu ve içerik üzerinde aşağıdaki biçimlendirme değişikliklerini uygulayan bir yapıyı göstermektedir:

Yazıda

Kod:Tümünü seç

[vurgulama]
shortcode’u varsa, aktif ayarların uygulanması (aksi halde shortcode kaldırılır).
İlk paragrafın içeriğini kalın (strong) yapma.
Büyük harfli kelimeleri, tırnak içindeki metinleri ve sayısal değerleri, admin panelde seçilebilen stil (kalın, renkli veya highlight) ile vurgulama.
Admin panelinde tüm ayarları (aktif/pasif, stil seçenekleri vb.) yapılandırma imkanı.
Aşağıdaki kodu örneğin “

Kod:Tümünü seç

vurgulama-shortcode.php
” adında bir dosyaya yapıştırıp, wp-content/plugins klasörüne ekleyip eklentiyi aktifleştirdikten sonra kullanabilirsiniz:

Kod:Tümünü seç

<?php
/*
Plugin Name: Vurgulama Shortcode
Description: [vurgulama] shortcode’u eklenen yazının içeriğinde okuma kolaylığı sağlayacak biçimlendirmeleri uygular.
Version: 1.0
Author: ChatGPT
*/

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Varsayılan ayarları tanımla ve eklenti aktif edilirken kaydet.
 */
function vurgulama_activate() {
    $default = array(
        'active'                => 0, // Başlangıçta pasif
        'first_paragraph_bold'  => 1,
        'uppercase'             => array( 'enabled' => 1, 'style' => 'bold' ),
        'quotes'                => array( 'enabled' => 1, 'style' => 'highlight' ),
        'numbers'               => array( 'enabled' => 1, 'style' => 'bold' ),
    );
    if ( ! get_option( 'vurgulama_settings' ) ) {
        add_option( 'vurgulama_settings', $default );
    }
}
register_activation_hook( __FILE__, 'vurgulama_activate' );

/**
 * Yönetim paneline ayar sayfası ekle
 */
function vurgulama_add_admin_menu() {
    add_options_page(
        'Vurgulama Ayarları',
        'Vurgulama',
        'manage_options',
        'vurgulama-settings',
        'vurgulama_settings_page'
    );
}
add_action( 'admin_menu', 'vurgulama_add_admin_menu' );

/**
 * Ayar sayfası HTML’i
 */
function vurgulama_settings_page() {
    if ( ! current_user_can( 'manage_options' ) ) {
        return;
    }
    
    // Form gönderildiğinde ayarları kaydet
    if ( isset( $_POST['vurgulama_settings_submit'] ) ) {
        check_admin_referer( 'vurgulama_settings_save', 'vurgulama_settings_nonce' );
        $settings = array(
            'active'               => isset( $_POST['active'] ) ? 1 : 0,
            'first_paragraph_bold' => isset( $_POST['first_paragraph_bold'] ) ? 1 : 0,
            'uppercase'            => array(
                'enabled' => isset( $_POST['uppercase_enabled'] ) ? 1 : 0,
                'style'   => in_array( $_POST['uppercase_style'], array( 'bold', 'colored', 'highlight' ) ) ? $_POST['uppercase_style'] : 'bold',
            ),
            'quotes'               => array(
                'enabled' => isset( $_POST['quotes_enabled'] ) ? 1 : 0,
                'style'   => in_array( $_POST['quotes_style'], array( 'bold', 'colored', 'highlight' ) ) ? $_POST['quotes_style'] : 'highlight',
            ),
            'numbers'              => array(
                'enabled' => isset( $_POST['numbers_enabled'] ) ? 1 : 0,
                'style'   => in_array( $_POST['numbers_style'], array( 'bold', 'colored', 'highlight' ) ) ? $_POST['numbers_style'] : 'bold',
            ),
        );
        update_option( 'vurgulama_settings', $settings );
        echo '<div class="updated"><p>Ayarlar kaydedildi.</p></div>';
    }
    
    $settings = get_option( 'vurgulama_settings' );
    ?>
    <div class="wrap">
        <h1>Vurgulama Ayarları</h1>
        <form method="post" action="">
            <?php wp_nonce_field( 'vurgulama_settings_save', 'vurgulama_settings_nonce' ); ?>
            <table class="form-table">
                <tr valign="top">
                    <th scope="row">Uygulama Aktif/Pasif</th>
                    <td><input type="checkbox" name="active" <?php checked( $settings['active'], 1 ); ?> /></td>
                </tr>
                <tr valign="top">
                    <th scope="row">İlk Paragrafı Kalın Yap</th>
                    <td><input type="checkbox" name="first_paragraph_bold" <?php checked( $settings['first_paragraph_bold'], 1 ); ?> /></td>
                </tr>
                <tr valign="top">
                    <th scope="row">Büyük Harfli Kelimeler</th>
                    <td>
                        <input type="checkbox" name="uppercase_enabled" <?php checked( $settings['uppercase']['enabled'], 1 ); ?> /> Etkin<br />
                        Stil:
                        <select name="uppercase_style">
                            <option value="bold" <?php selected( $settings['uppercase']['style'], 'bold' ); ?>>Kalın</option>
                            <option value="colored" <?php selected( $settings['uppercase']['style'], 'colored' ); ?>>Renkli</option>
                            <option value="highlight" <?php selected( $settings['uppercase']['style'], 'highlight' ); ?>>Highlight</option>
                        </select>
                    </td>
                </tr>
                <tr valign="top">
                    <th scope="row">Tırnak İçindeki Metin</th>
                    <td>
                        <input type="checkbox" name="quotes_enabled" <?php checked( $settings['quotes']['enabled'], 1 ); ?> /> Etkin<br />
                        Stil:
                        <select name="quotes_style">
                            <option value="bold" <?php selected( $settings['quotes']['style'], 'bold' ); ?>>Kalın</option>
                            <option value="colored" <?php selected( $settings['quotes']['style'], 'colored' ); ?>>Renkli</option>
                            <option value="highlight" <?php selected( $settings['quotes']['style'], 'highlight' ); ?>>Highlight</option>
                        </select>
                    </td>
                </tr>
                <tr valign="top">
                    <th scope="row">Sayısal Değerler</th>
                    <td>
                        <input type="checkbox" name="numbers_enabled" <?php checked( $settings['numbers']['enabled'], 1 ); ?> /> Etkin<br />
                        Stil:
                        <select name="numbers_style">
                            <option value="bold" <?php selected( $settings['numbers']['style'], 'bold' ); ?>>Kalın</option>
                            <option value="colored" <?php selected( $settings['numbers']['style'], 'colored' ); ?>>Renkli</option>
                            <option value="highlight" <?php selected( $settings['numbers']['style'], 'highlight' ); ?>>Highlight</option>
                        </select>
                    </td>
                </tr>
            </table>
            <?php submit_button( 'Ayarları Kaydet', 'primary', 'vurgulama_settings_submit' ); ?>
        </form>
    </div>
    <?php
}

/**
 * [vurgulama] shortcode’unu tanımla.
 * Bu shortcode içerik içinde sadece bir işaret görevi görür.
 */
function vurgulama_shortcode_handler( $atts, $content = null ) {
    return ''; // shortcode marker'ını içerikten kaldırıyoruz
}
add_shortcode( 'vurgulama', 'vurgulama_shortcode_handler' );

/**
 * Belirlenen stil seçeneklerine göre metni sarmalayan yardımcı fonksiyon.
 */
function vurgulama_wrap_text( $text, $style ) {
    switch ( $style ) {
        case 'bold':
            return '<strong>' . $text . '</strong>';
        case 'colored':
            return '<span style="color: red;">' . $text . '</span>';
        case 'highlight':
            return '<mark>' . $text . '</mark>';
        default:
            return $text;
    }
}

/**
 * Yazı içeriğini filtreleyip, ayarlara göre düzenleyen ana fonksiyon.
 */
function vurgulama_process_content( $content ) {
    // Eğer içerikte [vurgulama] shortcode’u yoksa hiçbir işlem yapma
    if ( ! has_shortcode( $content, 'vurgulama' ) ) {
        return $content;
    }
    
    $settings = get_option( 'vurgulama_settings' );
    // Eğer eklenti pasif durumdaysa, sadece shortcode'u kaldır
    if ( empty( $settings['active'] ) ) {
        $content = do_shortcode( $content );
        return $content;
    }
    
    // Önce shortcode marker’ını kaldırıyoruz
    $content = do_shortcode( $content );
    
    // 1. İlk paragrafı kalın yap
    if ( ! empty( $settings['first_paragraph_bold'] ) ) {
        // İlk <p> etiketini yakalayıp içeriğini <strong> ile sarmala (sadece ilk eşleşmede)
        $content = preg_replace_callback( '/(<p\b[^>]*>)(.*?)(<\/p>)/is', function( $matches ) {
            return $matches[1] . '<strong>' . $matches[2] . '</strong>' . $matches[3];
        }, $content, 1 );
    }
    
    // 2. Büyük harfli kelimeleri vurgula
    if ( ! empty( $settings['uppercase']['enabled'] ) ) {
        $style = $settings['uppercase']['style'];
        // En az 2 harften oluşan tüm tamamen büyük harfli kelimeler için
        $content = preg_replace_callback( '/\b([A-Z]{2,})\b/', function( $matches ) use ( $style ) {
            return vurgulama_wrap_text( $matches[0], $style );
        }, $content );
    }
    
    // 3. Tırnak içerisindeki metni vurgula
    if ( ! empty( $settings['quotes']['enabled'] ) ) {
        $style = $settings['quotes']['style'];
        // Çift tırnak içine alınan metinler
        $content = preg_replace_callback( '/"([^"]+)"/', function( $matches ) use ( $style ) {
            return '"' . vurgulama_wrap_text( $matches[1], $style ) . '"';
        }, $content );
    }
    
    // 4. Sayısal değerleri vurgula
    if ( ! empty( $settings['numbers']['enabled'] ) ) {
        $style = $settings['numbers']['style'];
        // Tam sayılar için
        $content = preg_replace_callback( '/\b(\d+)\b/', function( $matches ) use ( $style ) {
            return vurgulama_wrap_text( $matches[0], $style );
        }, $content );
    }
    
    return $content;
}
add_filter( 'the_content', 'vurgulama_process_content' );


muratca61
Site Admin
Mesajlar: 35964
Kayıt: Cmt Ara 21, 2024 7:56 am

Re: wordpress vurgulama eklentisi

Mesaj gönderen muratca61 »

sadece main loop etki

Kod:Tümünü seç

<?php
/*
Plugin Name: Vurgulama Shortcode
Description: [vurgulama] shortcode’u eklenen yazının kendi içeriğinde okuma kolaylığı sağlayacak biçimlendirmeleri uygular.
Version: 1.1
Author: ChatGPT
*/

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Varsayılan ayarları tanımla ve eklenti aktif edilirken kaydet.
 */
function vurgulama_activate() {
    $default = array(
        'active'                => 0, // Başlangıçta pasif
        'first_paragraph_bold'  => 1,
        'uppercase'             => array( 'enabled' => 1, 'style' => 'bold' ),
        'quotes'                => array( 'enabled' => 1, 'style' => 'highlight' ),
        'numbers'               => array( 'enabled' => 1, 'style' => 'bold' ),
    );
    if ( ! get_option( 'vurgulama_settings' ) ) {
        add_option( 'vurgulama_settings', $default );
    }
}
register_activation_hook( __FILE__, 'vurgulama_activate' );

/**
 * Yönetim paneline ayar sayfası ekle.
 */
function vurgulama_add_admin_menu() {
    add_options_page(
        'Vurgulama Ayarları',
        'Vurgulama',
        'manage_options',
        'vurgulama-settings',
        'vurgulama_settings_page'
    );
}
add_action( 'admin_menu', 'vurgulama_add_admin_menu' );

/**
 * Ayar sayfası HTML’i.
 */
function vurgulama_settings_page() {
    if ( ! current_user_can( 'manage_options' ) ) {
        return;
    }
    
    // Form gönderildiğinde ayarları kaydet
    if ( isset( $_POST['vurgulama_settings_submit'] ) ) {
        check_admin_referer( 'vurgulama_settings_save', 'vurgulama_settings_nonce' );
        $settings = array(
            'active'               => isset( $_POST['active'] ) ? 1 : 0,
            'first_paragraph_bold' => isset( $_POST['first_paragraph_bold'] ) ? 1 : 0,
            'uppercase'            => array(
                'enabled' => isset( $_POST['uppercase_enabled'] ) ? 1 : 0,
                'style'   => in_array( $_POST['uppercase_style'], array( 'bold', 'colored', 'highlight' ) ) ? $_POST['uppercase_style'] : 'bold',
            ),
            'quotes'               => array(
                'enabled' => isset( $_POST['quotes_enabled'] ) ? 1 : 0,
                'style'   => in_array( $_POST['quotes_style'], array( 'bold', 'colored', 'highlight' ) ) ? $_POST['quotes_style'] : 'highlight',
            ),
            'numbers'              => array(
                'enabled' => isset( $_POST['numbers_enabled'] ) ? 1 : 0,
                'style'   => in_array( $_POST['numbers_style'], array( 'bold', 'colored', 'highlight' ) ) ? $_POST['numbers_style'] : 'bold',
            ),
        );
        update_option( 'vurgulama_settings', $settings );
        echo '<div class="updated"><p>Ayarlar kaydedildi.</p></div>';
    }
    
    $settings = get_option( 'vurgulama_settings' );
    ?>
    <div class="wrap">
        <h1>Vurgulama Ayarları</h1>
        <form method="post" action="">
            <?php wp_nonce_field( 'vurgulama_settings_save', 'vurgulama_settings_nonce' ); ?>
            <table class="form-table">
                <tr valign="top">
                    <th scope="row">Uygulama Aktif/Pasif</th>
                    <td><input type="checkbox" name="active" <?php checked( $settings['active'], 1 ); ?> /></td>
                </tr>
                <tr valign="top">
                    <th scope="row">İlk Paragrafı Kalın Yap</th>
                    <td><input type="checkbox" name="first_paragraph_bold" <?php checked( $settings['first_paragraph_bold'], 1 ); ?> /></td>
                </tr>
                <tr valign="top">
                    <th scope="row">Büyük Harfli Kelimeler</th>
                    <td>
                        <input type="checkbox" name="uppercase_enabled" <?php checked( $settings['uppercase']['enabled'], 1 ); ?> /> Etkin<br />
                        Stil:
                        <select name="uppercase_style">
                            <option value="bold" <?php selected( $settings['uppercase']['style'], 'bold' ); ?>>Kalın</option>
                            <option value="colored" <?php selected( $settings['uppercase']['style'], 'colored' ); ?>>Renkli</option>
                            <option value="highlight" <?php selected( $settings['uppercase']['style'], 'highlight' ); ?>>Highlight</option>
                        </select>
                    </td>
                </tr>
                <tr valign="top">
                    <th scope="row">Tırnak İçindeki Metin</th>
                    <td>
                        <input type="checkbox" name="quotes_enabled" <?php checked( $settings['quotes']['enabled'], 1 ); ?> /> Etkin<br />
                        Stil:
                        <select name="quotes_style">
                            <option value="bold" <?php selected( $settings['quotes']['style'], 'bold' ); ?>>Kalın</option>
                            <option value="colored" <?php selected( $settings['quotes']['style'], 'colored' ); ?>>Renkli</option>
                            <option value="highlight" <?php selected( $settings['quotes']['style'], 'highlight' ); ?>>Highlight</option>
                        </select>
                    </td>
                </tr>
                <tr valign="top">
                    <th scope="row">Sayısal Değerler</th>
                    <td>
                        <input type="checkbox" name="numbers_enabled" <?php checked( $settings['numbers']['enabled'], 1 ); ?> /> Etkin<br />
                        Stil:
                        <select name="numbers_style">
                            <option value="bold" <?php selected( $settings['numbers']['style'], 'bold' ); ?>>Kalın</option>
                            <option value="colored" <?php selected( $settings['numbers']['style'], 'colored' ); ?>>Renkli</option>
                            <option value="highlight" <?php selected( $settings['numbers']['style'], 'highlight' ); ?>>Highlight</option>
                        </select>
                    </td>
                </tr>
            </table>
            <?php submit_button( 'Ayarları Kaydet', 'primary', 'vurgulama_settings_submit' ); ?>
        </form>
    </div>
    <?php
}

/**
 * [vurgulama] shortcode’unu tanımla.
 * Bu shortcode içerik içinde sadece işaret görevi görür.
 */
function vurgulama_shortcode_handler( $atts, $content = null ) {
    return ''; // shortcode marker'ını içerikten kaldırıyoruz
}
add_shortcode( 'vurgulama', 'vurgulama_shortcode_handler' );

/**
 * Belirlenen stil seçeneklerine göre metni sarmalayan yardımcı fonksiyon.
 */
function vurgulama_wrap_text( $text, $style ) {
    switch ( $style ) {
        case 'bold':
            return '<strong>' . $text . '</strong>';
        case 'colored':
            return '<span style="color: red;">' . $text . '</span>';
        case 'highlight':
            return '<mark>' . $text . '</mark>';
        default:
            return $text;
    }
}

/**
 * Yazı içeriğini filtreleyip, ayarlara göre düzenleyen ana fonksiyon.
 * Bu fonksiyon yalnızca ana döngüde ve ana sorguda çalışır.
 */
function vurgulama_process_content( $content ) {
    // Yalnızca ana döngüde (main loop) ve ana sorguda işlem yapalım
    if ( ! in_the_loop() || ! is_main_query() ) {
        return $content;
    }
    
    // Eğer içerikte [vurgulama] shortcode’u yoksa, içerik üzerinde işlem yapma.
    if ( ! has_shortcode( $content, 'vurgulama' ) ) {
        return $content;
    }
    
    $settings = get_option( 'vurgulama_settings' );
    // Eğer eklenti pasif ise, sadece shortcode'u kaldırıyoruz.
    if ( empty( $settings['active'] ) ) {
        $content = do_shortcode( $content );
        return $content;
    }
    
    // Önce shortcode marker’ını kaldırıyoruz.
    $content = do_shortcode( $content );
    
    // 1. İlk paragrafı kalın yap
    if ( ! empty( $settings['first_paragraph_bold'] ) ) {
        $content = preg_replace_callback( '/(<p\b[^>]*>)(.*?)(<\/p>)/is', function( $matches ) {
            return $matches[1] . '<strong>' . $matches[2] . '</strong>' . $matches[3];
        }, $content, 1 );
    }
    
    // 2. Büyük harfli kelimeleri vurgula
    if ( ! empty( $settings['uppercase']['enabled'] ) ) {
        $style = $settings['uppercase']['style'];
        $content = preg_replace_callback( '/\b([A-Z]{2,})\b/', function( $matches ) use ( $style ) {
            return vurgulama_wrap_text( $matches[0], $style );
        }, $content );
    }
    
    // 3. Tırnak içerisindeki metni vurgula
    if ( ! empty( $settings['quotes']['enabled'] ) ) {
        $style = $settings['quotes']['style'];
        $content = preg_replace_callback( '/"([^"]+)"/', function( $matches ) use ( $style ) {
            return '"' . vurgulama_wrap_text( $matches[1], $style ) . '"';
        }, $content );
    }
    
    // 4. Sayısal değerleri vurgula
    if ( ! empty( $settings['numbers']['enabled'] ) ) {
        $style = $settings['numbers']['style'];
        $content = preg_replace_callback( '/\b(\d+)\b/', function( $matches ) use ( $style ) {
            return vurgulama_wrap_text( $matches[0], $style );
        }, $content );
    }
    
    return $content;
}
add_filter( 'the_content', 'vurgulama_process_content' );
muratca61
Site Admin
Mesajlar: 35964
Kayıt: Cmt Ara 21, 2024 7:56 am

Re: wordpress vurgulama eklentisi

Mesaj gönderen muratca61 »

Kod:Tümünü seç

<?php
/*
Plugin Name: Vurgulama Shortcode
Description: [vurgulama] shortcode’u eklenen yazının <p> etiketleri içerisindeki metninde okuma kolaylığı sağlayacak biçimlendirmeleri uygular. Linklere müdahale etmez.
Version: 1.6
Author: ChatGPT
*/

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Varsayılan ayarları tanımlar ve eklenti aktif edilirken kaydeder.
 */
function vurgulama_activate() {
    $default = array(
        'active'                => 0, // Başlangıçta pasif
        'first_paragraph_bold'  => 1,
        'uppercase'             => array( 'enabled' => 1, 'style' => 'bold' ),
        'quotes'                => array( 'enabled' => 1, 'style' => 'highlight' ),
        'numbers'               => array( 'enabled' => 1, 'style' => 'bold' ),
    );
    if ( ! get_option( 'vurgulama_settings' ) ) {
        add_option( 'vurgulama_settings', $default );
    }
}
register_activation_hook( __FILE__, 'vurgulama_activate' );

/**
 * Yönetim paneline ayar sayfası ekler.
 */
function vurgulama_add_admin_menu() {
    add_options_page(
        'Vurgulama Ayarları',
        'Vurgulama',
        'manage_options',
        'vurgulama-settings',
        'vurgulama_settings_page'
    );
}
add_action( 'admin_menu', 'vurgulama_add_admin_menu' );

/**
 * Ayar sayfası HTML’i.
 */
function vurgulama_settings_page() {
    if ( ! current_user_can( 'manage_options' ) ) {
        return;
    }
    
    // Form gönderildiğinde ayarları kaydet
    if ( isset( $_POST['vurgulama_settings_submit'] ) ) {
        check_admin_referer( 'vurgulama_settings_save', 'vurgulama_settings_nonce' );
        $settings = array(
            'active'               => isset( $_POST['active'] ) ? 1 : 0,
            'first_paragraph_bold' => isset( $_POST['first_paragraph_bold'] ) ? 1 : 0,
            'uppercase'            => array(
                'enabled' => isset( $_POST['uppercase_enabled'] ) ? 1 : 0,
                'style'   => in_array( $_POST['uppercase_style'], array( 'bold', 'colored', 'highlight' ) ) ? $_POST['uppercase_style'] : 'bold',
            ),
            'quotes'               => array(
                'enabled' => isset( $_POST['quotes_enabled'] ) ? 1 : 0,
                'style'   => in_array( $_POST['quotes_style'], array( 'bold', 'colored', 'highlight' ) ) ? $_POST['quotes_style'] : 'highlight',
            ),
            'numbers'              => array(
                'enabled' => isset( $_POST['numbers_enabled'] ) ? 1 : 0,
                'style'   => in_array( $_POST['numbers_style'], array( 'bold', 'colored', 'highlight' ) ) ? $_POST['numbers_style'] : 'bold',
            ),
        );
        update_option( 'vurgulama_settings', $settings );
        echo '<div class="updated"><p>Ayarlar kaydedildi.</p></div>';
    }
    
    $settings = get_option( 'vurgulama_settings' );
    ?>
    <div class="wrap">
        <h1>Vurgulama Ayarları</h1>
        <form method="post" action="">
            <?php wp_nonce_field( 'vurgulama_settings_save', 'vurgulama_settings_nonce' ); ?>
            <table class="form-table">
                <tr valign="top">
                    <th scope="row">Uygulama Aktif/Pasif</th>
                    <td><input type="checkbox" name="active" <?php checked( $settings['active'], 1 ); ?> /></td>
                </tr>
                <tr valign="top">
                    <th scope="row">İlk Paragrafı Kalın Yap</th>
                    <td><input type="checkbox" name="first_paragraph_bold" <?php checked( $settings['first_paragraph_bold'], 1 ); ?> /></td>
                </tr>
                <tr valign="top">
                    <th scope="row">Büyük Harfli Kelimeler</th>
                    <td>
                        <input type="checkbox" name="uppercase_enabled" <?php checked( $settings['uppercase']['enabled'], 1 ); ?> /> Etkin<br />
                        Stil:
                        <select name="uppercase_style">
                            <option value="bold" <?php selected( $settings['uppercase']['style'], 'bold' ); ?>>Kalın</option>
                            <option value="colored" <?php selected( $settings['uppercase']['style'], 'colored' ); ?>>Renkli</option>
                            <option value="highlight" <?php selected( $settings['uppercase']['style'], 'highlight' ); ?>>Highlight</option>
                        </select>
                    </td>
                </tr>
                <tr valign="top">
                    <th scope="row">Tırnak İçindeki Metin</th>
                    <td>
                        <input type="checkbox" name="quotes_enabled" <?php checked( $settings['quotes']['enabled'], 1 ); ?> /> Etkin<br />
                        Stil:
                        <select name="quotes_style">
                            <option value="bold" <?php selected( $settings['quotes']['style'], 'bold' ); ?>>Kalın</option>
                            <option value="colored" <?php selected( $settings['quotes']['style'], 'colored' ); ?>>Renkli</option>
                            <option value="highlight" <?php selected( $settings['quotes']['style'], 'highlight' ); ?>>Highlight</option>
                        </select>
                    </td>
                </tr>
                <tr valign="top">
                    <th scope="row">Sayısal Değerler</th>
                    <td>
                        <input type="checkbox" name="numbers_enabled" <?php checked( $settings['numbers']['enabled'], 1 ); ?> /> Etkin<br />
                        Stil:
                        <select name="numbers_style">
                            <option value="bold" <?php selected( $settings['numbers']['style'], 'bold' ); ?>>Kalın</option>
                            <option value="colored" <?php selected( $settings['numbers']['style'], 'colored' ); ?>>Renkli</option>
                            <option value="highlight" <?php selected( $settings['numbers']['style'], 'highlight' ); ?>>Highlight</option>
                        </select>
                    </td>
                </tr>
            </table>
            <?php submit_button( 'Ayarları Kaydet', 'primary', 'vurgulama_settings_submit' ); ?>
        </form>
    </div>
    <?php
}

/**
 * [vurgulama] shortcode’unu tanımlar.
 * Bu shortcode içerik içinde sadece işaret görevi görür.
 */
function vurgulama_shortcode_handler( $atts, $content = null ) {
    return ''; // shortcode marker'ını içerikten kaldırıyoruz
}
add_shortcode( 'vurgulama', 'vurgulama_shortcode_handler' );

/**
 * Belirlenen stil seçeneklerine göre metni sarmalar.
 */
function vurgulama_wrap_text( $text, $style ) {
    switch ( $style ) {
        case 'bold':
            return '<strong>' . $text . '</strong>';
        case 'colored':
            return '<span style="color: red;">' . $text . '</span>';
        case 'highlight':
            return '<mark>' . $text . '</mark>';
        default:
            return $text;
    }
}

/**
 * Yazı içeriğini, yalnızca <p> etiketleri içerisindeki metne uygulayacak şekilde filtreler.
 * - İşlem yalnızca tekil yazı/sayfa, ana döngü ve ana sorgu kapsamında çalışır.
 * - Linkler (<a> etiketleri) korunur.
 * - Önce metindeki HTML entity’ler (ör. &#039;) gerçek karakterlere dönüştürülür.
 * - Büyük harfli kelimeler için, ilk harf dizisi [A-Za-z]+ kısmı (ilk harflerden sonraki, harf olmayan karaktere kadar)
 *   vurgulanır; dolayısıyla "Trabzon&#039;da" ifadesinde yalnızca "Trabzon" vurgulanır.
 */
function vurgulama_process_content( $content ) {
    // Yalnızca tekil içeriklerde ve ana döngü/sorguda çalışsın
    if ( ! is_singular() || ! in_the_loop() || ! is_main_query() ) {
        return $content;
    }
    
    // [vurgulama] shortcode'u yoksa düzenleme yapma
    if ( ! has_shortcode( $content, 'vurgulama' ) ) {
        return $content;
    }
    
    $settings = get_option( 'vurgulama_settings' );
    // Eklenti pasifse, yalnızca shortcode marker'ını kaldır
    if ( empty( $settings['active'] ) ) {
        $content = do_shortcode( $content );
        return $content;
    }
    
    // Önce shortcode marker'ını kaldırıyoruz
    $content = do_shortcode( $content );
    
    $firstParagraphProcessed = false;
    // Sadece <p> etiketleri içindeki metin üzerinde çalışalım
    $content = preg_replace_callback('/(<p\b[^>]*>)(.*?)(<\/p>)/is', function( $matches ) use ( $settings, &$firstParagraphProcessed ) {
        $p_start   = $matches[1];
        $p_content = $matches[2];
        $p_end     = $matches[3];
        
        // Linkleri (<a> etiketlerini) korumak için geçici token kullanıyoruz
        $anchor_array = array();
        $p_content = preg_replace_callback('/<a\b[^>]*>.*?<\/a>/is', function($m) use (&$anchor_array) {
            $token = '%%ANCHOR_' . count($anchor_array) . '%%';
            $anchor_array[$token] = $m[0];
            return $token;
        }, $p_content);
        
        // HTML entity'leri gerçek karakterlere dönüştür (ör. &#039; -> ')
        $p_content = html_entity_decode($p_content, ENT_QUOTES, 'UTF-8');
        
        // Eğer ilk paragrafsa ve ayar aktifse, tüm paragrafı <strong> ile sarmala
        if ( ! $firstParagraphProcessed && ! empty( $settings['first_paragraph_bold'] ) ) {
            $p_content = '<strong>' . $p_content . '</strong>';
            $firstParagraphProcessed = true;
        }
        
        // Büyük harfli kelimelerde – artık yalnızca harflerden oluşan ilk diziyi (harf olmayan karaktere kadar) eşle
        if ( ! empty( $settings['uppercase']['enabled'] ) ) {
            $style_option = $settings['uppercase']['style'];
            $p_content = preg_replace_callback(
                '/\b([A-Za-z]+)(?=[^A-Za-z]|$)/',
                function($m) use ($style_option) {
                    // Eğer kelimenin tamamı istenen formattaysa (ör. tamamen büyük veya baş harfi büyük)
                    // isteğe bağlı olarak burada ek kontrol eklenebilir.
                    return vurgulama_wrap_text($m[1], $style_option);
                },
                $p_content
            );
        }
        
        // Tırnak içerisindeki metni vurgula (iki " arasındaki içerik)
        if ( ! empty( $settings['quotes']['enabled'] ) ) {
            $style_option = $settings['quotes']['style'];
            $p_content = preg_replace_callback(
                '/"([^"]+)"/',
                function($m) use ($style_option) {
                    return '"' . vurgulama_wrap_text($m[1], $style_option) . '"';
                },
                $p_content
            );
        }
        
        // Sayısal değerleri vurgula
        if ( ! empty( $settings['numbers']['enabled'] ) ) {
            $style_option = $settings['numbers']['style'];
            $p_content = preg_replace_callback(
                '/\b(\d+)\b/',
                function($m) use ($style_option) {
                    return vurgulama_wrap_text($m[0], $style_option);
                },
                $p_content
            );
        }
        
        // İşlemler tamamlandıktan sonra <a> etiketlerini geri yerleştir
        if ( ! empty( $anchor_array ) ) {
            foreach ( $anchor_array as $token => $anchor_html ) {
                $p_content = str_replace( $token, $anchor_html, $p_content );
            }
        }
        
        return $p_start . $p_content . $p_end;
    }, $content);
    
    return $content;
}
add_filter( 'the_content', 'vurgulama_process_content' );
muratca61
Site Admin
Mesajlar: 35964
Kayıt: Cmt Ara 21, 2024 7:56 am

Re: wordpress vurgulama eklentisi

Mesaj gönderen muratca61 »

Kod:Tümünü seç

<?php
/*
Plugin Name: Vurgulama Shortcode
Description: [vurgulama] shortcode’u eklenen yazının <p> etiketleri içerisindeki metninde okuma kolaylığı sağlayacak biçimlendirmeleri uygular. Linklere müdahale etmez.
Version: 1.7
Author: ChatGPT
*/

if (!defined('ABSPATH')) {
    exit;
}

/**
 * Varsayılan ayarları tanımlar ve eklenti aktif edilirken kaydeder.
 */
function vurgulama_activate()
{
    $default = array(
        'active'                => 0,
        'first_paragraph_bold'  => 1,
        'uppercase'             => array('enabled' => 1, 'style' => 'bold'),
        'quotes'                => array('enabled' => 1, 'style' => 'highlight'),
        'numbers'               => array('enabled' => 1, 'style' => 'bold'),
    );
    if (!get_option('vurgulama_settings')) {
        add_option('vurgulama_settings', $default);
    }
}
register_activation_hook(__FILE__, 'vurgulama_activate');

/**
 * Yönetim paneline ayar sayfası ekler.
 */
function vurgulama_add_admin_menu()
{
    add_options_page(
        'Vurgulama Ayarları',
        'Vurgulama',
        'manage_options',
        'vurgulama-settings',
        'vurgulama_settings_page'
    );
}
add_action('admin_menu', 'vurgulama_add_admin_menu');

/**
 * Ayar sayfası HTML'i.
 */
function vurgulama_settings_page()
{
    if (!current_user_can('manage_options')) {
        return;
    }

    if (isset($_POST['vurgulama_settings_submit'])) {
        check_admin_referer('vurgulama_settings_save', 'vurgulama_settings_nonce');
        $settings = array(
            'active'               => isset($_POST['active']) ? 1 : 0,
            'first_paragraph_bold' => isset($_POST['first_paragraph_bold']) ? 1 : 0,
            'uppercase'            => array(
                'enabled' => isset($_POST['uppercase_enabled']) ? 1 : 0,
                'style'   => in_array($_POST['uppercase_style'], array('bold', 'colored', 'highlight')) ? $_POST['uppercase_style'] : 'bold',
            ),
            'quotes'               => array(
                'enabled' => isset($_POST['quotes_enabled']) ? 1 : 0,
                'style'   => in_array($_POST['quotes_style'], array('bold', 'colored', 'highlight')) ? $_POST['quotes_style'] : 'highlight',
            ),
            'numbers'              => array(
                'enabled' => isset($_POST['numbers_enabled']) ? 1 : 0,
                'style'   => in_array($_POST['numbers_style'], array('bold', 'colored', 'highlight')) ? $_POST['numbers_style'] : 'bold',
            ),
        );
        update_option('vurgulama_settings', $settings);
        echo '<div class="updated"><p>Ayarlar kaydedildi.</p></div>';
    }

    $settings = get_option('vurgulama_settings');
?>
    <div class="wrap">
        <h1>Vurgulama Ayarları</h1>
        <form method="post" action="">
            <?php wp_nonce_field('vurgulama_settings_save', 'vurgulama_settings_nonce'); ?>
            <table class="form-table">
                <tr valign="top">
                    <th scope="row">Uygulama Aktif/Pasif</th>
                    <td><input type="checkbox" name="active" <?php checked($settings['active'], 1); ?> /></td>
                </tr>
                <tr valign="top">
                    <th scope="row">İlk Paragrafı Kalın Yap</th>
                    <td><input type="checkbox" name="first_paragraph_bold" <?php checked($settings['first_paragraph_bold'], 1); ?> /></td>
                </tr>
                <tr valign="top">
                    <th scope="row">Büyük Harfli Kelimeler</th>
                    <td>
                        <input type="checkbox" name="uppercase_enabled" <?php checked($settings['uppercase']['enabled'], 1); ?> /> Etkin<br />
                        Stil:
                        <select name="uppercase_style">
                            <option value="bold" <?php selected($settings['uppercase']['style'], 'bold'); ?>>Kalın</option>
                            <option value="colored" <?php selected($settings['uppercase']['style'], 'colored'); ?>>Renkli</option>
                            <option value="highlight" <?php selected($settings['uppercase']['style'], 'highlight'); ?>>Highlight</option>
                        </select>
                    </td>
                </tr>
                <tr valign="top">
                    <th scope="row">Tırnak İçindeki Metin</th>
                    <td>
                        <input type="checkbox" name="quotes_enabled" <?php checked($settings['quotes']['enabled'], 1); ?> /> Etkin<br />
                        Stil:
                        <select name="quotes_style">
                            <option value="bold" <?php selected($settings['quotes']['style'], 'bold'); ?>>Kalın</option>
                            <option value="colored" <?php selected($settings['quotes']['style'], 'colored'); ?>>Renkli</option>
                            <option value="highlight" <?php selected($settings['quotes']['style'], 'highlight'); ?>>Highlight</option>
                        </select>
                    </td>
                </tr>
                <tr valign="top">
                    <th scope="row">Sayısal Değerler</th>
                    <td>
                        <input type="checkbox" name="numbers_enabled" <?php checked($settings['numbers']['enabled'], 1); ?> /> Etkin<br />
                        Stil:
                        <select name="numbers_style">
                            <option value="bold" <?php selected($settings['numbers']['style'], 'bold'); ?>>Kalın</option>
                            <option value="colored" <?php selected($settings['numbers']['style'], 'colored'); ?>>Renkli</option>
                            <option value="highlight" <?php selected($settings['numbers']['style'], 'highlight'); ?>>Highlight</option>
                        </select>
                    </td>
                </tr>
            </table>
            <?php submit_button('Ayarları Kaydet', 'primary', 'vurgulama_settings_submit'); ?>
        </form>
    </div>
<?php
}

/**
 * [vurgulama] shortcode'unu tanımlar.
 */
function vurgulama_shortcode_handler($atts, $content = null)
{
    return '';
}
add_shortcode('vurgulama', 'vurgulama_shortcode_handler');

/**
 * Belirlenen stil seçeneklerine göre metni sarmalar.
 */
function vurgulama_wrap_text($text, $style)
{
    switch ($style) {
        case 'bold':
            return '<strong>' . $text . '</strong>';
        case 'colored':
            return '<span style="color: red;">' . $text . '</span>';
        case 'highlight':
            return '<mark>' . $text . '</mark>';
        default:
            return $text;
    }
}

/**
 * İçerik işleme fonksiyonu (GÜNCELLENDİ)
 */
function vurgulama_process_content($content)
{
    if (!is_singular() || !in_the_loop() || !is_main_query()) {
        return $content;
    }

    if (!has_shortcode($content, 'vurgulama')) {
        return $content;
    }

    $settings = get_option('vurgulama_settings');
    if (empty($settings['active'])) {
        $content = do_shortcode($content);
        return $content;
    }

    $content = do_shortcode($content);

    $firstParagraphProcessed = false;
    $content = preg_replace_callback('/(<p\b[^>]*>)(.*?)(<\/p>)/is', function ($matches) use ($settings, &$firstParagraphProcessed) {
        $p_start   = $matches[1];
        $p_content = $matches[2];
        $p_end     = $matches[3];

        $anchor_array = array();
        $p_content = preg_replace_callback('/<a\b[^>]*>.*?<\/a>/is', function ($m) use (&$anchor_array) {
            $token = '%%ANCHOR_' . count($anchor_array) . '%%';
            $anchor_array[$token] = $m[0];
            return $token;
        }, $p_content);

        $p_content = html_entity_decode($p_content, ENT_QUOTES, 'UTF-8');

        if (!$firstParagraphProcessed && !empty($settings['first_paragraph_bold'])) {
            $p_content = '<strong>' . $p_content . '</strong>';
            $firstParagraphProcessed = true;
        }

        // BÜYÜK HARFLER İÇİN GÜNCELLENMİŞ KISIM
        if (!empty($settings['uppercase']['enabled'])) {
            $style_option = $settings['uppercase']['style'];
            $p_content = preg_replace_callback(
                '/\b(\p{Lu}+)\b/u', // TAMAMEN BÜYÜK HARFLİ KELİMELER
                function ($m) use ($style_option) {
                    return vurgulama_wrap_text($m[1], $style_option);
                },
                $p_content
            );
        }

        if (!empty($settings['quotes']['enabled'])) {
            $style_option = $settings['quotes']['style'];
            $p_content = preg_replace_callback(
                '/"([^"]+)"/',
                function ($m) use ($style_option) {
                    return '"' . vurgulama_wrap_text($m[1], $style_option) . '"';
                },
                $p_content
            );
        }

        if (!empty($settings['numbers']['enabled'])) {
            $style_option = $settings['numbers']['style'];
            $p_content = preg_replace_callback(
                '/\b(\d+)\b/',
                function ($m) use ($style_option) {
                    return vurgulama_wrap_text($m[0], $style_option);
                },
                $p_content
            );
        }

        if (!empty($anchor_array)) {
            foreach ($anchor_array as $token => $anchor_html) {
                $p_content = str_replace($token, $anchor_html, $p_content);
            }
        }

        return $p_start . $p_content . $p_end;
    }, $content);

    return $content;
}
add_filter('the_content', 'vurgulama_process_content');
Cevapla