• I have problem with this code… I want to make plugin that will automaticaly add https://shorted.ga/’.$userid.’/’.$type.’/ just after ‘href=”‘ – This is something like adfly Easy Link but I want to make wordpress pluggin for it… I made everything – I just don’t know what should I edit here to make my script save settings…

    `<?php

    /*

    Plugin Name: Shorted.ga Easy Link pluggin

    Plugin URI: https://shorted.ga/

    Description: Just simple, if you use this plugins, your Shorted.ga EASY LINK will be added automatically, for all the external links of your website posts or pages.

    Version: 1.0

    Author: John

    Author URI: https://shorted.ga/

    License: GPL2

    */

    function register_my_setting() {

    register_setting( ‘shorted’, ‘shortedgaid-info-settings’ );

    register_setting( ‘shorted’, ‘shortedgatip-info-settings’ );

    }

    add_action( ‘admin_init’, ‘register_my_setting’ );

    add_filter( ‘the_content’, ‘cn_nf_url_parse2’);

    function cn_nf_url_parse2( $content ) {

    $tip = get_option( ‘shortedgatip_info’ );

    $id = get_option( ‘shortedga_info’ );

    $content = str_replace(‘href=”‘, ‘href=”https://shorted.ga/&#8217;.$id.’/’.$tip.’/’, $content);

    return $content;

    }

    add_action( ‘admin_menu’, ‘register_shortedga_menu_page’ );

    function register_shortedga_menu_page(){

    add_menu_page( ‘Shorted Menu’, ‘Shorted Menu’, ‘manage_options’, ‘shorted-easy-link-pluggin/shorted-easy-link-pluggin.php’, ‘shortedga_menu_page’, ”, 6 );

    }

    function shortedga_menu_page(){

    ?><h1>Shorted.ga Menu Options</h1>

    <form method=”post” action=”options.php”>

    <?php settings_fields( ‘shortedgaid-info-settings’ ); ?>

    <?php do_settings_sections( ‘shortedgaid-info-settings’ ); ?>

    <?php settings_fields( ‘shortedgatip-info-settings’ ); ?>

    <?php do_settings_sections( ‘shortedgatip-info-settings’ ); ?>

    <table class=”form-table”>

    <tr valign=”top”>

    <th scope=”row”>Shorted.ga Menu Options</th>

    </tr> <tr valign=”top”> <td>Please add your Shorted.ga ID<br /><input type=”text” name=”‘shortedgaid-info” value=”<?php echo get_option( ‘shortedgaid-info’ ); ?>”/></td>

    </tr> <tr valign=”top”>

    <td>Please write correct type of links “0” for Family Safe and “1” for Adult links<br /><input type=”text” name=”shortedgatip-info” value=”<?php echo get_option( ‘shortedgatip-info’ ); ?>”/></td>

    </tr>

    </table>

    <?php submit_button(); ?></form>

    <?php

    }`

    I always get ERROR: options page not found.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Sloppy code dude. You must to read docs carefully before you do something for very first time Creating Options Page

    Replace your settings code with this:

    add_action( 'admin_menu', 'register_shortedga_menu' );
    function register_shortedga_menu()
    {
    
        // Add as submenu item of Settings main menu
        add_submenu_page(
            'options-general.php',
            'Shorted.ga',
            'Shorted.ga',
            'manage_options',
            'shortedga',
            'shortedga_settings_page'
        );
    
        // Register settings at admin_init only on options pages
        if (
            !empty( $GLOBALS['pagenow'] )
            and (
                'options-general.php' === $GLOBALS['pagenow']
                or 'options.php' === $GLOBALS['pagenow']
            )
        )
        {
            add_action( 'admin_init', 'register_shortedga_setting' );
        }
    
    } // eof register_shortedga_menu
    
    function register_shortedga_setting()
    {
    
        // Now do register setting
        register_setting(
            'shortedga', // Group, used for settings_fields()
            'shortedga' // Option name, used as key in database
            // It's a good practice to do option sanitization here
        );
    
    } // eof register_shortedga_setting
    
    function shortedga_settings_page()
    {
    
    ?><h2><?php echo $GLOBALS['title']; ?> Options</h2>
    
    <form method="post" action="options.php">
    <?php
        // Prepare form sections
        settings_fields( 'shortedga' );
        do_settings_sections( 'shortedga' );
    
        // Fetch existing options
        $option_values = get_option( 'shortedga' );
    ?>
    <table class="form-table">
        <tr>
            <th scope="row">
                <label for="shortedga[id]">Your <em>Shorted.ga</em> ID</label>
            </th>
            <td>
                <input type="text" name="shortedga[id]" id="shortedga[id]" class="regular-text" value="<?php echo $option_values['id']; ?>" />
            </td>
        </tr>
        <tr>
            <th scope="row">
                <label for="shortedga[tip]">Type of Links</label>
            </th>
            <td>
                <select name="shortedga[tip]" id="shortedga[tip]">
                    <option>-- Select Proper Option --</option>
                    <option value="0" <?php selected( $option_values['tip'], 0 );?>>Family Safe</option>
                    <option value="1" <?php selected( $option_values['tip'], 1 );?>>Adults Only</option>
                </select>
            </td>
        </tr>
    </table>
    
    <?php submit_button(); ?>
    </form>
    
    <?php
    
    } // eof shortedga_settings_page

    And then get option in your function with $option = get_option( 'shortedga' );

    Btw, never place Option page with couple options in admin root but as submenu of Settings or Tools items.

    Cheers,
    Urke

    Thread Starter john888999

    (@john888999)

    I did it with this code

    <?php
    
    /*
    
    Plugin Name: Shorted.ga Easy Link plugin
    
    Plugin URI: https://shorted.ga/
    
    Description: Just simple, if you use this plugins, your Shorted.ga EASY LINK will be added automatically, for all the external links of your website posts or pages.
    
    Version: 1.0
    
    Author: John
    
    Author URI: https://shorted.ga/
    
    License: GPL2
    
    */
    
    add_filter( 'the_content', 'cn_nf_url_parse2');
    
    function cn_nf_url_parse2( $content ) {
    
    	$tip = get_option( 'shortedga-tip' );
    
    	$id = get_option( 'shortedga-id' );
    
    	$content = str_replace('href="', 'href="https://shorted.ga/'.$id.'/'.$tip.'/', $content);
    
    	return $content;
    
    }
    
    add_action( 'admin_menu', 'register_shortedga_menu_page' );
    
    function register_shortedga_menu_page(){
    
        add_menu_page( 'Shorted Menu', 'Shorted Menu', 'manage_options', 'shorted-easy-link-plugin/shorted-easy-link-plugin.php', 'shortedga_menu_page', '', 6 );
    
    }
    
    function shortedga_menu_page(){
    
     ?><h1>Shorted.ga Menu Options</h1>
    
     <form method="post" action="options.php">
    
       <?php settings_fields( 'shortedga-id' ); ?>
    
        <?php do_settings_sections( 'shortedga-id' ); ?>
    
        <table class="form-table">
    
          <tr valign="top">
    
          <th scope="row">Shorted.ga Menu Options</th>
    
         </tr>  <tr valign="top"> <td>Please add your Shorted.ga ID<br /><input type="text" name="shortedga-id" value="<?php echo get_option( 'shortedga-id' ); ?>"/></td>
    
    	    </tr>
        </table>
    
        <?php submit_button(); ?></form>
    
    <form method="post" action="options.php">
    
    	<?php settings_fields( 'shortedga-tip' ); ?>
    
        <?php do_settings_sections( 'shortedga-tip' ); ?>
    
        <table class="form-table">
    
    <tr valign="top">
    
    <td>Please write correct type of links "0" for Family Safe and "1" for Adult links<br /><input type="text" name="shortedga-tip" value="<?php echo get_option( 'shortedga-tip' ); ?>"/></td>
    
          </tr>
    
        </table>
    
        <?php submit_button(); ?></form>
    
    <?php
    }
    
    function register_mysettings() { // whitelist options
     	 register_setting( 'shortedga-id', 'shortedga-id' );
    	 register_setting( 'shortedga-tip', 'shortedga-tip' );
    }
    add_action( 'admin_init', 'register_mysettings' );

    `
    This works perfect for me and for every version of wordpress. How can I post it to Plugin page???

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Plugin for URL shortener’ is closed to new replies.