Thursday, December 22, 2022
HomeWordPress Developmentposts - SQL Test if a phrase is enclosed in sq. brackets...

posts – SQL Test if a phrase is enclosed in sq. brackets and convert it to a hyperlink


The problem is to search out phrases enclosed in dobule sq. brackets, for [[example]], and switch them into hyperlinks, writing to the database (wp_posts desk).

For instance, if [[dog]], or [[car]], then

<a href="https://instance.com/canine/">canine</a>
<a href="https://instance.com/automobile/">automobile</a>
<a href="https://instance.com/social-sciences/">Social Sciences</a>

The identical phrase is the slug. The magic is in turning that phrase right into a slug, or duplicating it, and creating the hyperlink.

This plugin labored in older variations of WordPress, nevertheless it does not anymore.

<?php
require_once ABSPATH . WPINC . "/publish.php";

if ( !class_exists("WikiLinksPlugin") ) {
class WikiLinksPlugin {

    var $identify = "WikiPageLinksPlugin";
    var $shortName = "WikiPageLinks";
    var $longName = "Wiki Web page Hyperlinks Plugin";
    var $adminOptionsName = "WikiPageLinksPluginAdminOptions";
    
    var $debug = false;

    var $defaultShortcuts = array(
        '' => 'https://instance.com/%s/',
    );
    
    perform log($message) {
        if ($this->debug)
            error_log($message . "n", 3, "/tmp/wikilinks.log");
    
    }
    
    //PHP4 constructor
    perform WikiLinksPlugin() {$this->__construct();} 
    
    //PHP5 constructor
    perform __construct() {
        // WordPress Hooks
        //add_action('admin_menu', array(&$this, 'addAdminPanel'));  
        add_filter('the_content', array(&$this, 'wiki_filter'));
    }
    
    perform _install() {
        $this->log("Wiki Hyperlinks put in!");
    }
    
    perform _uninstall() {
        $this->log("Wiki Hyperlinks uninstalled!");
    }
    
    /**
     * Added by Daniel Llewellyn (Fremen):
     * separate out the seen title from the hyperlink identify from wikilinks of the shape [[link|some user title]]
     */
    perform wiki_get_piped_title($hyperlink) ', $hyperlink, 2);
        if (!$title) $title = $hyperlink;
        return array($hyperlink, $title);
    

    /* The filter.
     * Replaces double brackets with hyperlinks to pages.
     */
    perform wiki_filter($content material) {
        $choices = $this->getAdminOptions();

        //Match solely phrases in double brackets.  A backslash could be
        //used to flee the sequence, in order for you literal double brackets.
        preg_match_all('/[[([^]]+)]]/', $content material, $matches);

        //$matches[1] is an array of all of the phrases in double brackets.
        //Dumping all of the matches right into a hash ensures we solely search for
        //every matching web page identify as soon as.
        $hyperlinks = array();
        foreach( $matches[1] as $key phrase ) {
            $hyperlinks[$keyword] = present($matches[0]);
            subsequent($matches[0]);
        }

        foreach( $hyperlinks as $full_link => $match ) {
            // If the "web page title" accommodates a ':', it *could* be a shortcut
            // hyperlink quite than a web page.  Cope with these first.
            record($prefix, $sublink) = explode(':', $full_link, 2);

            if ( $sublink ) {
                if ( array_key_exists($prefix, $choices['shortcuts']) ) {
                    record($hyperlink, $subtitle) = $this->wiki_get_piped_title($sublink);
                    $shortcutLink = sprintf( $choices['shortcuts'][$prefix],
                        rawurlencode($hyperlink));
                    $content material = str_replace($match, 
                        "<sturdy><a href="https://wordpress.stackexchange.com/questions/412285/$shortcutLink">$subtitle</sturdy></a>",
                        $content material);
                    proceed;
                }
            }
            
            record($hyperlink, $page_title) = $this->wiki_get_piped_title($full_link);

            //We've got a web page hyperlink. 
            //TODO: lower down on db hits and get the record of pages as a substitute.
            if ( $web page = get_page_by_title(html_entity_decode($hyperlink, ENT_QUOTES)) ) {
                $content material = str_replace($match, 
                    "<a href="". get_permalink($page->ID) ."">$page_title</a>",
                    $content material);
            } else if ( is_user_logged_in() ) {
                //Add a hyperlink to create the web page if it does not exist.
                //TODO: restrict displaying the hyperlink to customers who can create posts.

                $house = get_option('siteurl');
                $encodedlink = urlencode($hyperlink);
                $content material = str_replace($match, "{$page_title}[<a href="$home/wp-admin/post-new.php?post_type=page&post_title=$encodedlink" class="nonexistant_page" title="Create this page (requires a valid "contributer" account)">?</a>]", $content material);

            } else {
                
                $content material = str_replace($match, $page_title, $content material);
            }
        }
        
        return $content material;
    }

    perform getAdminOptions() {
        //defaults
        $choices = array(
            'shortcuts' => $this->defaultShortcuts,
        );
    
        $savedOptions = get_option($this->adminOptionsName);
        
        if (!empty($savedOptions)) {
            foreach ($savedOptions as $key => $worth) {
                $choices[$key] = $worth;
            }
        } 
        
        return $choices;
    
    }
    
    perform saveAdminOptions($choices) {
        if (get_option($this->adminOptionsName)) {
            $this->log("Updating choice: " . $this->adminOptionsName);
            update_option($this->adminOptionsName, $choices);
        } else {
            $this->log("Including new choice: " . $this->adminOptionsName);
            add_option($this->adminOptionsName, $choices);
        }
    }
        
    perform adminPanel() {  
        $adminOptions = $this->getAdminOptions();
        $submitButton = "submit_Save${shortname}Choices";

        if (isset($_POST[$submitButton])) {
            for ( $i=0; $i < $this->shortcutCount; $i++ ) {
                if (isset($_POST["shortcut$i"])) {
                    $adminOptions['feeds'][$i] = $_POST["feeds$i"];
                }
            }
            
            print_r($adminOptions);
            $this->saveAdminOptions($adminOptions);
            
            ?>
            
        <div class="up to date"><p><sturdy>
        <?php  _e("Settings Up to date", $this->identify); ?>
        </sturdy></p></div>
        
            <?php
        }
        
        ?>  
        <div class="wrap">  
            <h2><?php echo $longName; ?></h2>  
            <type methodology="publish" motion="<?php echo $_SERVER['REQUEST_URI']; ?>">
            <?php wp_nonce_field('update-options'); ?>
            
            <desk class="form-table">
            <tr valign="prime">
            
            <th scope="row"></th>
            <td>

            <enter sort="textual content" 
                   identify="feeds<?php echo $i; ?>" 
                   worth="<?php echo $feed; ?>"
                   dimension="50" />
            <br />
            
            </td>
            </tr>

            </desk>
            
            <enter sort="hidden" identify="motion" worth="replace" />
            <enter sort="hidden" identify="page_options"
             worth="new_option_name,some_other_option,option_etc" />

            <p class="submit">
            <enter sort="submit" 
                identify="<?php echo $submitButton; ?>" 
                worth="<?php _e('Save Modifications'); ?>" />
            </p>

            
            </type>
        </div>  
        <?php  
    }    
    
    perform addAdminPanel()   
    {  
        add_submenu_page('options-general.php', 
        $this->longName, 
        $this->shortName, 
        10, __FILE__, 
        array(&$this, 'adminPanel'));
    }    

}
}


if (class_exists(WikiLinksPlugin) && !isset($wikiLinks_plugin)) {
    $wikiLinks_plugin = new WikiLinksPlugin();    
}

?>

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments