Advertisement
Guest User

Untitled

a guest
Dec 31st, 2014
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.24 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Fallback URL
  4. Plugin URI: http://diegopeinador.com/fallback-url-yourls-plugin
  5. Description: This plugin allows you to define a fallback URL in case there isn't a match for your short URL, so you can specify something different than $YOURLS_HOME.
  6. Version: 1.0
  7. Author: Diego Peinador
  8. Author URI: http://diegopeinador.com
  9. */
  10.  
  11. // No direct call
  12. if( !defined( 'YOURLS_ABSPATH' ) ) die();
  13.  
  14. yourls_add_action( 'redirect_keyword_not_found', 'dp_fallback_url' );
  15. function dp_fallback_url() {
  16.         // Get value from database
  17.         $fallback_url = yourls_get_option( 'fallback_url' );
  18.     yourls_redirect( $fallback_url, 302 ); //Use a temporal redirect in case there is a valid keyword in the future
  19. }
  20.  
  21. // Register our plugin config page
  22. yourls_add_action( 'plugins_loaded', 'dp_config_add_page' );
  23. function dp_config_add_page() {
  24.         yourls_register_plugin_page( 'fallback_url_config', 'Fallback URL Plugin Config', 'dp_config_do_page' );
  25.         // parameters: page slug, page title, and function that will display the page itself
  26. }
  27.  
  28. // Display config page
  29. function dp_config_do_page() {
  30.  
  31.         // Check if a form was submitted
  32.         if( isset( $_POST['fallback_url'] ) )
  33.                 dp_config_update_option();
  34.  
  35.         // Get value from database
  36.         $fallback_url = yourls_get_option( 'fallback_url' );
  37.  
  38.         echo <<<HTML
  39.                 <h2>Fallback URL Plugin Config</h2>
  40.                 <p>Here you can configure the URL to redirect in case the keyword is not found in database.</p>
  41.                 <form method="post">
  42.                 <p><label for="fallback_url">URL to fallback to</label> <input type="text" id="fallback_url" name="fallback_url" value="$fallback_url" size="40" /></p>
  43.                 <p><input type="submit" value="Update value" /></p>
  44.                 </form>
  45. HTML;
  46. }
  47.  
  48. // Update option in database
  49. function dp_config_update_option() {
  50.         $in = $_POST['fallback_url'];
  51.  
  52.         if( $in ) {
  53.                 // Validate test_option. ALWAYS validate and sanitize user input.
  54.                 // Here, we want an string
  55.                 $in = strval( $in);
  56.  
  57.                 // Update value in database
  58.                 yourls_update_option( 'fallback_url', $in );
  59.         }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement