Skip to content

Instantly share code, notes, and snippets.

@artero
Created April 9, 2012 08:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artero/2342385 to your computer and use it in GitHub Desktop.
Save artero/2342385 to your computer and use it in GitHub Desktop.
Wordpress cookbook
<?php
/*
Plugin Name: Settings API Options Example
Plugin URI: http://www.presscoders.com/
Description: Shows an example implementation of each input field type
Author: David Gwyer
Author URI: http://www.presscoders.com/
*/
// Specify Hooks/Filters
register_activation_hook(__FILE__, 'add_defaults_fn');
add_action('admin_init', 'sampleoptions_init_fn' );
add_action('admin_menu', 'sampleoptions_add_page_fn');
// Define default option settings
function add_defaults_fn() {
$tmp = get_option('plugin_options');
if(($tmp['chkbox1']=='on')||(!is_array($tmp))) {
$arr = array("dropdown1"=>"Orange", "text_area" => "Space to put a lot of information here!", "text_string" => "Some sample text", "pass_string" => "123456", "chkbox1" => "", "chkbox2" => "on", "option_set1" => "Triangle");
update_option('plugin_options', $arr);
}
}
// Register our settings. Add the settings section, and settings fields
function sampleoptions_init_fn(){
register_setting('plugin_options', 'plugin_options', 'plugin_options_validate' );
add_settings_section('main_section', 'Main Settings', 'section_text_fn', __FILE__);
add_settings_field('plugin_text_string', 'Text Input', 'setting_string_fn', __FILE__, 'main_section');
add_settings_field('plugin_text_pass', 'Password Text Input', 'setting_pass_fn', __FILE__, 'main_section');
add_settings_field('plugin_textarea_string', 'Large Textbox!', 'setting_textarea_fn', __FILE__, 'main_section');
add_settings_field('plugin_chk2', 'A Checkbox', 'setting_chk2_fn', __FILE__, 'main_section');
add_settings_field('radio_buttons', 'Select Shape', 'setting_radio_fn', __FILE__, 'main_section');
add_settings_field('drop_down1', 'Select Color', 'setting_dropdown_fn', __FILE__, 'main_section');
add_settings_field('plugin_chk1', 'Restore Defaults Upon Reactivation?', 'setting_chk1_fn', __FILE__, 'main_section');
}
// Add sub page to the Settings Menu
function sampleoptions_add_page_fn() {
add_options_page('Options Example Page', 'Options Example', 'administrator', __FILE__, 'options_page_fn');
}
// ************************************************************************************************************
// Callback functions
// Section HTML, displayed before the first option
function section_text_fn() {
echo '<p>Below are some examples of different option controls.</p>';
}
// DROP-DOWN-BOX - Name: plugin_options[dropdown1]
function setting_dropdown_fn() {
$options = get_option('plugin_options');
$items = array("Red", "Green", "Blue", "Orange", "White", "Violet", "Yellow");
echo "<select id='drop_down1' name='plugin_options[dropdown1]'>";
foreach($items as $item) {
$selected = ($options['dropdown1']==$item) ? 'selected="selected"' : '';
echo "<option value='$item' $selected>$item</option>";
}
echo "</select>";
}
// TEXTAREA - Name: plugin_options[text_area]
function setting_textarea_fn() {
$options = get_option('plugin_options');
echo "<textarea id='plugin_textarea_string' name='plugin_options[text_area]' rows='7' cols='50' type='textarea'>{$options['text_area']}</textarea>";
}
// TEXTBOX - Name: plugin_options[text_string]
function setting_string_fn() {
$options = get_option('plugin_options');
echo "<input id='plugin_text_string' name='plugin_options[text_string]' size='40' type='text' value='{$options['text_string']}' />";
}
// PASSWORD-TEXTBOX - Name: plugin_options[pass_string]
function setting_pass_fn() {
$options = get_option('plugin_options');
echo "<input id='plugin_text_pass' name='plugin_options[pass_string]' size='40' type='password' value='{$options['pass_string']}' />";
}
// CHECKBOX - Name: plugin_options[chkbox1]
function setting_chk1_fn() {
$options = get_option('plugin_options');
if($options['chkbox1']) { $checked = ' checked="checked" '; }
echo "<input ".$checked." id='plugin_chk1' name='plugin_options[chkbox1]' type='checkbox' />";
}
// CHECKBOX - Name: plugin_options[chkbox2]
function setting_chk2_fn() {
$options = get_option('plugin_options');
if($options['chkbox2']) { $checked = ' checked="checked" '; }
echo "<input ".$checked." id='plugin_chk2' name='plugin_options[chkbox2]' type='checkbox' />";
}
// RADIO-BUTTON - Name: plugin_options[option_set1]
function setting_radio_fn() {
$options = get_option('plugin_options');
$items = array("Square", "Triangle", "Circle");
foreach($items as $item) {
$checked = ($options['option_set1']==$item) ? ' checked="checked" ' : '';
echo "<label><input ".$checked." value='$item' name='plugin_options[option_set1]' type='radio' /> $item</label><br />";
}
}
// Display the admin options page
function options_page_fn() {
?>
<div class="wrap">
<div class="icon32" id="icon-options-general"><br></div>
<h2>My Example Options Page</h2>
Some optional text here explaining the overall purpose of the options and what they relate to etc.
<form action="options.php" method="post">
<?php settings_fields('plugin_options'); ?>
<?php do_settings_sections(__FILE__); ?>
<p class="submit">
<input name="Submit" type="submit" class="button-primary" value="<?php esc_attr_e('Save Changes'); ?>" />
</p>
</form>
</div>
<?php
}
// Validate user data for some/all of your input fields
function plugin_options_validate($input) {
// Check our textbox option field contains no HTML tags - if so strip them out
$input['text_string'] = wp_filter_nohtml_kses($input['text_string']);
return $input; // return validated input
}

Plugins

Qtranslate

Pasoso para activa: 1 - Descargar los archivos de idiomas del repositorio de WP 2 - Copiar los archivos de lenguage en wp-content/languages

Si hay problemas en la paginación se puede hacer:

// Elimina la redireccion automatica remove_action('template_redirect', 'redirect_canonical');

//Filtros para corregir correctamente, segun el idioma, los links las "pages" y los "post" add_filter('get_pagenum_link', 'qtrans_convertURL'); add_filter('post_type_link', 'qtrans_convertURL');

Funciones interesantes:

  • Seleccionar idioma:
  • Para ver la variable de idioma:

wpalchemy-metabox

http://www.farinspace.com/wpalchemy-metabox/ Para crear metaboxes de un post-type

CPT-onomies: Using Custom Post Types as Taxonomies

Para usar post-types como taxonomias http://wordpress.org/extend/plugins/cpt-onomies/

No Future Posts

Para hacer post-types como agendas http://www.tomsdimension.de/wp-plugins/no-future-posts

[Por probar] Facebook:

https://developers.facebook.com/wordpress/

wp-scb-framework

https://github.com/scribu/wp-scb-framework/wiki Útil para crear opciones y funcionalidades desde el administrador

class model {
var $name = 'events';
var $labels = array(
'labels' => array(
'name' => 'Eventos',
'singular_name' => 'Evento',
'add_new' => 'Nuevo',
'menu_name' => 'Eventos'
),
//'description' => '...',
'public' => true,
'has_archive' => true,
'menu_position' => 9,
//'menu_icon' => 'http://...',
'capability_type' => 'post'
);
function create_post_type(){
register_post_type($this->name, $this->labels);
}
}
add_action('init', function(){
add_rewrite_rule( 'doggy/([^/]+)/([^/]+)/([^/]+)', 'index.php?post_type=doggy&size=$matches[1]&color=$matches[2]&hair=$matches[3]', 'top' ); // first param in regular expresions, in the third param you define if this rule have priority over the wp default rules 'top' = is a priority
// You can't use $_GET['size'],
// you need $wp_query->query_vars['size'],
// and you need:
add_rewrite_tag('%size%','([^&]+)');
add_rewrite_tag('%color%','([^&]+)');
add_rewrite_tag('%hair%','([^&]+)');
// redefine the regrite rules, only use in init hoock
global $wp_rewrite;
flush_rewrite_rules();
});
global $wp_query;
$total = $wp_query->max_num_pages;
// only bother with the rest if we have more than 1 page!
if ( $total > 1 ) {
// get the current page
if ( !$current_page = get_query_var('paged') )
$current_page = 1;
// structure of "format" depends on whether we're using pretty permalinks
$format = empty( get_option('permalink_structure') ) ? '&page=%#%' : 'page/%#%/';
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => $format,
'current' => $current_page,
'total' => $total,
'mid_size' => 4,
'type' => 'list'
));
}

Pagination

ref: http://www.wprecipes.com/easy-wordpress-pagination-without-plugins

global $wp_query;
$total = $wp_query->max_num_pages;
// only bother with the rest if we have more than 1 page!
if ( $total > 1 )  {
     // get the current page
     if ( !$current_page = get_query_var('paged') )
          $current_page = 1;
     // structure of "format" depends on whether we're using pretty permalinks
     $format = empty( get_option('permalink_structure') ) ? '&page=%#%' : 'page/%#%/';
     echo paginate_links(array(
          'base' => get_pagenum_link(1) . '%_%',
          'format' => $format,
          'current' => $current_page,
          'total' => $total,
          'mid_size' => 4,
          'type' => 'list'
     ));
}

Use a page into another page

New WP_Query solution

ref: http://www.wprecipes.com/wordpress-hack-embed-a-page-into-another-page

<?php $recent = new WP_Query("page_id=**ID**"); while($recent->have_posts()) : $recent->the_post();?>
       <h3><?php the_title(); ?></h3>
       <?php the_content(); ?>
<?php endwhile; ?>

## Function solution using { get_page_by_path() }

function show_post($path) {
  $post = get_page_by_path($path);
  $content = apply_filters('the_content', $post->post_content);
  echo $content;
}
...
<?php show_post('about');  // Shows the content of the "About" page. ?>
<?php show_post('products/widget1');  // Shows content of the "Products > Widget" page. ?>

Generalice the previus solution 1:

function show_post($path, $activeFilter) {
  $post = get_page_by_path($path);
  $content = apply_filters($activeFilter, $post->post_content);
  echo $content;
}
...
<?php show_post('about', 'the_title');  // Shows the content of the "About" page. ?>
<?php show_post('products/widget1', 'the_content');  // Shows content of the "Products > Widget" page. ?>

The Rewrite API

Create a new URI estructure

You can define a new rewrite rule using the add_rewrite_rule() function, sample:

//
add_acction('init', function(){
    add_rewrite_rule( 'doggy/([^/]+)/([^/]+)/([^/]+)', 'index.php?post_type=doggy&size=$matches[1]&color=$matches[2]&hair=$matches[3]', 'top' );  // first param in regular expresions, in the third param you define if this rule have priority over the wp default rules 'top' = is a priority

    // You can't use $_GET['size'], 
    // you need $wp_query->query_vars['size'],
    // and you need:
    add_rewrite_tag('%size%','([^&]+)');
    add_rewrite_tag('%color%','([^&]+)');
    add_rewrite_tag('%hair%','([^&]+)');
});
global $wp_rewrite;
flush_rewrite_rules(); // redefine the regrite rules, only use in init hoock

Ref:

Páginas de Opciones

Ref:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment