Site icon Hip-Hop Website Design and Development

Change customized submit sort slug from plugin choices

I might like to provide customers the choice to vary a customized submit sort slug in my plugin. Utilizing this reply, I can hardcode it right into a perform:

Redeclare/Change Slug of a Plugin's Customized Put up Kind

So how can I rewrite this to replace when the person updates the plugin possibility? I attempted one thing like this:

perform add_custom_rewrite_rule() {

$slug = get_option('change_rewrite_slug'); // my plugin possibility

// First, attempt to load up the rewrite guidelines. We do that simply in case
// the default permalink construction is getting used.
if( ($current_rules = get_option('rewrite_rules')) ) {

    // Subsequent, iterate via every customized rule including a brand new rule
    // that replaces 'films' with 'movies' and provides it a better
    // precedence than the present rule.
    foreach($current_rules as $key => $val) {
        if(strpos($key, 'my_post_type') !== false) {
            add_rewrite_rule(str_ireplace('my_post_type', $slug, $key), $val, 'high');   
        } // finish if
    } // finish foreach

} // finish if/else

// ...and we flush the principles
flush_rewrite_rules();

} // finish add_custom_rewrite_rule
add_action('init', 'add_custom_rewrite_rule');

This does not work. I really feel like I’m lacking a step: as soon as the plugin possibility is saved it wants to inform this perform to fireplace and get the brand new slug. Or perhaps there’s a higher technique to do it.

Additionally, as soon as it’s modified, I want the perform to replace that, get the present possibility after which change the slug primarily based on any new enter.

The rationale I want that is that the plugin has a submit sort of 1 form of occasion however I would like individuals to have the ability to customise it so as a substitute of ‘occasion’ the slug may very well be ‘present’ or ‘gig’ or no matter they need.

Replace: I am utilizing this class: http://tareq.wedevs.com/2012/06/wordpress-settings-api-php-class/

Here’s a part of the choices web page code:

 perform flush_settings() {

    if( isset( $_REQUEST['settings-updated'] ) )
    flush_rewrite_rules();
}

/**
 * Returns all of the settings fields
 *
 * @return array settings fields
 */
perform get_settings_fields() {
    $settings_fields = array(

    // Different settings right here
        'djgigs_basic_settings' => array(
            array(
                'identify' => 'djgigs_rewrite_slug',
                'label' => __( 'Permalink Slug', 'wedevs' ),
                'desc' => __( 'Enter a customized permalink slug. The default is 'djgig' however you would change it to 'gigs' or 'occasions'. ', 'wedevs' ),
                'sort' => 'textual content',
                'default' => 'djgig',
            ),
        )

    );

    return $settings_fields;
}

Unsure if that’s the appropriate location so as to add the flush perform or if there’s something within the Class that’s making this not work.