Snippet PHP Code

Lot of Source

Old .html to wordpress post name

  1. # BEGIN WordPress
  2. <IfModule mod_rewrite.c>
  3. RewriteEngine On
  4. RewriteBase /
  5. RewriteRule (.+)\.html?$ http://www.example.com/$1/ [R=301,L]
  6. RewriteRule ^index\.php$ - [L]
  7. RewriteCond %{REQUEST_FILENAME} !-f
  8. RewriteCond %{REQUEST_FILENAME} !-d
  9. RewriteRule . /index.php [L]
  10. </IfModule>
  11. # END WordPress

Limit Content

  1. function excerpt($limit) {
  2. $excerpt = explode(' ', get_the_excerpt(), $limit);
  3. if (count($excerpt)>=$limit) {
  4. array_pop($excerpt);
  5. $excerpt = implode(" ",$excerpt).'...';
  6. } else {
  7. $excerpt = implode(" ",$excerpt);
  8. }    
  9. $excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
  10. return $excerpt;
  11. }
  12. function content($limit) {
  13. $content = explode(' ', get_the_content(), $limit);
  14. if (count($content)>=$limit) {
  15. array_pop($content);
  16. $content = implode(" ",$content).'...';
  17. } else {
  18. $content = implode(" ",$content);
  19. }    
  20. $content = preg_replace('/\[.+\]/','', $content);
  21. $content = apply_filters('the_content', $content);
  22. $content = str_replace(']]>', ']]&gt;', $content);
  23. return $content;
  24. }
  25. // Usage Content limit
  26. <?php echo excerpt(25); ?>
  27. <?php echo content(25); ?>

Define Directory

  1. define('THEMEXXX', get_template_directory_uri().'/js/');
  2. wp_enqueue_script('some_class',THEMEXXX.'xxx.js',array(),'1.0',true);

Function Check

  1. if(!function_exists('xxx')):
  2. function xxx()
  3. {
  4. if(is_admin())
  5. {
  6. // Function goes to here
  7. }
  8. }
  9. add_action('admin_enqueue_scripts','xxx');
  10. endif;

Custom Function

  1. function custom_function () {
  2. echo 'XXXXX';
  3. echo 'XXXXX';
  4. }
  5. add_action('wp_head', 'custom_function');

Remove unwanted items

  1. remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0); // Display the links to the shortlink
  2. remove_action( 'wp_head', 'rel_canonical'); // Display the links to the canonical link
  3. remove_action( 'wp_head', 'feed_links_extra', 3); // Display the links to the extra feeds such as category feeds
  4. remove_action( 'wp_head', 'feed_links', 2); // Display the links to the general feeds: Post and Comment Feed
  5. remove_action( 'wp_head', 'rsd_link'); // Display the link to the Really Simple Discovery service endpoint, EditURI link
  6. remove_action( 'wp_head', 'wlwmanifest_link' ); // Display the link to the Windows Live Writer manifest file.
  7. remove_action( 'wp_head', 'index_rel_link' ); // index link
  8. remove_action( 'wp_head', 'parent_post_rel_link'); // prev link
  9. remove_action( 'wp_head', 'start_post_rel_link'); // start link
  10. remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0); // Display relational links for the posts adjacent to the current post.
  11. remove_action( 'wp_head', 'wp_generator'); // Display the XHTML generator that is generated on the wp_head hook, WP version    

Customize admin footer text

  1. function custom_admin_footer() {
  2. echo '<p>Example</p>';
  3. }
  4. add_filter('admin_footer_text', 'custom_admin_footer');

PHP CSS

  1. <?php
  2. header('Content-type: text/css');
  3. $parse_uri = explode('wp-content', $_SERVER['SCRIPT_FILENAME']);
  4. $wp_load = $parse_uri[0].'wp-load.php';
  5. require_once($wp_load);
  6. ?>

Themes Option Using PHP File

  1. global $smof_data;
  2. $output = '';
  3. if(isset($smof_data['body_bg'])):
  4.     $output .= 'body{ background: '.$smof_data['body_bg'] .'; }';
  5. endif;
  6. echo $output;

Themes Option Using Functions

  1. $box_options = pukka_fp_box_settings();
  2.     //add_image_size('thumb-content', 615, 9999, false); // single page
  3.     add_image_size('thumb-brick-big', $box_options['big_img_width'], $box_options['big_img_height'], true); // big brick
  4. --
  5. add_action('init', 'pyre_init');
  6. function pyre_init() {
  7.     global $data;
  8.     register_post_type(
  9.         'avada_portfolio',
  10.         array(
  11.             'labels' => array(
  12.                 'name' => 'Portfolio',
  13.                 'singular_name' => 'Portfolio'
  14.             ),
  15.             'public' => true,
  16.             'has_archive' => true,
  17.             'rewrite' => array('slug' => $data['portfolio_slug']),
  18.             'supports' => array('title', 'editor', 'thumbnail','comments'),
  19.             'can_export' => true,
  20.         )
  21.     );

Dynamic title

  1. function dynamic_title() {
  2. if (is_home()) {
  3. if (get_option('page_for_posts', true)) {
  4. return get_the_title(get_option('page_for_posts', true));
  5. } else {
  6. return get_bloginfo('name');
  7. }
  8. } elseif (is_archive()) {
  9. $term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));
  10. if ($term) {
  11. return apply_filters('single_term_title', $term->name);
  12. } elseif (is_post_type_archive()) {
  13. return apply_filters('the_title', get_queried_object()->labels->name);
  14. } elseif (is_day()) {
  15. return sprintf(__('Daily Archives: %s', 'rm2334'), get_the_date());
  16. } elseif (is_month()) {
  17. return sprintf(__('Monthly Archives: %s', 'rm2334'), get_the_date('F Y'));
  18. } elseif (is_year()) {
  19. return sprintf(__('Yearly Archives: %s', 'rm2334'), get_the_date('Y'));
  20. } elseif (is_author()) {
  21. $author = get_queried_object();
  22. return sprintf(__('Author Archives: %s', 'rm2334'), $author->display_name);
  23. } else {
  24. return single_cat_title('', false);
  25. }
  26. } elseif (is_search()) {
  27. return sprintf(__('Search Results for %s', 'rm2334'), get_search_query());
  28. } elseif (is_404()) {
  29. return __('Not Found', 'rm2334');
  30. } else {
  31. return get_the_title();
  32. }
  33. }

Multi Dropdown

  1. <?php $video_source = rwmb_meta( 'cookingwp_video_source' ); ?>
  2. <?php $video = rwmb_meta( 'cookingwp_video' ); ?>
  3. <?php if($video_source == 1): ?>
  4. <?php echo rwmb_meta( 'cookingwp_video' ); ?>
  5. <?php elseif ($video_source == 2): ?>
  6. <?php echo ''.$video.''; ?>
  7. <?php elseif ($video_source == 3): ?>
  8. <?php echo ''.$video.''; ?>
  9. <?php endif; ?>

.htaccess

  1. // .htaccess
  2. User-agent: *
  3. Disallow: /feed/
  4. Disallow: /trackback/
  5. Disallow: /wp-admin/
  6. Disallow: /wp-content/
  7. Disallow: /wp-includes/
  8. Disallow: /xmlrpc.php
  9. Disallow: /wp-
  10. Allow: /wp-content/uploads/
  11. Sitemap: http://example.com/sitemap.xml
  12. User-agent: *
  13. Disallow: /feed/
  14. Disallow: /cgi-bin/
  15. Disallow: /wp-admin/
  16. Disallow: /wp-content/
  17. Disallow: /wp-includes/
  18. Disallow: /trackback/
  19. Disallow: /xmlrpc.php
  20. Disallow: ?wptheme=
  21. Disallow: /blackhole/
  22. Disallow: /transfer/
  23. Disallow: /tweets/
  24. Disallow: /mint/
  25. Allow: /tag/mint/
  26. Allow: /tag/feed/
  27. Allow: /wp-content/online/
  28. Sitemap: http://perishablepress.com/sitemap-perish.xml
  29. Sitemap: http://perishablepress.com/sitemap-press.xml
  30. User-agent: ia_archiver
  31. Disallow: /
  32. User-agent: *
  33. Disallow: /mint/
  34. Disallow: /labs/
  35. Disallow: /*/wp-*
  36. Disallow: /*/feed/*
  37. Disallow: /*/*?s=*
  38. Disallow: /*/*.js$
  39. Disallow: /*/*.inc$
  40. Disallow: /transfer/
  41. Disallow: /*/cgi-bin/*
  42. Disallow: /*/blackhole/*
  43. Disallow: /*/trackback/*
  44. Disallow: /*/xmlrpc.php
  45. Allow: /*/20*/wp-*
  46. Allow: /press/feed/$
  47. Allow: /press/tag/feed/$
  48. Allow: /*/wp-content/online/*
  49. Sitemap: http://perishablepress.com/sitemap.xml
  50. User-agent: ia_archiver
  51. Disallow: /