Skip to content

Instantly share code, notes, and snippets.

@AndrewSavetchuk
Last active March 17, 2024 23:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save AndrewSavetchuk/6571db1871ef38c3c4aaf02f8315434f to your computer and use it in GitHub Desktop.
Save AndrewSavetchuk/6571db1871ef38c3c4aaf02f8315434f to your computer and use it in GitHub Desktop.

Display Posts From the Database

<?php /* Display posts from the database */ ?>
<?php if ( have_posts() ) : ?>
    <?php while ( have_posts() ) : the_post(); ?>

        <?php the_post_thumbnail_url(); ?>
        <?php echo get_the_date(); ?>
        <?php echo get_the_title(); ?>
        <?php the_excerpt(); ?>
        <?php echo esc_url( get_permalink() ); ?>

    <?php endwhile; ?>
<?php endif; ?>

Hide/Remove Items in the Admin Panel

Should be placed in functions.php.

<?php
// Remove the REST API endpoint
remove_action('rest_api_init', 'wp_oembed_register_route');

// Turn off oEmbed auto discovery
add_filter('embed_oembed_discover', '__return_false');

// Don't filter oEmbed results
remove_filter('oembed_dataparse', 'wp_filter_oembed_result', 10);

// Remove oEmbed discovery links
remove_action('wp_head', 'wp_oembed_add_discovery_links');

// Remove oEmbed-specific JavaScript from the front-end and back-end
remove_action('wp_head', 'wp_oembed_add_host_js');

// Remove all embeds rewrite rules
add_filter('rewrite_rules_array', 'disable_embeds_rewrites');

// Replace [...] with custom text
function new_excerpt_more($more)
{
    return ' ...';
}
add_filter('excerpt_more', 'new_excerpt_more');

// Return (n) words
function new_excerpt_length($length)
{
    return 50;
}
add_filter('excerpt_length', 'new_excerpt_length');

// Add a CSS class to the previous and next buttons
function post_link_attributes($output)
{
    $injection = 'class="my-class"';
    return str_replace('<a href=', '<a ' . $injection . ' href=', $output);
}
add_filter('next_post_link', 'post_link_attributes');
add_filter('previous_post_link', 'post_link_attributes');

// Remove Contact Form 7 Styles
function wps_deregister_styles()
{
    wp_deregister_style('contact-form-7');
}
add_action('wp_print_styles', 'wps_deregister_styles', 100);

// Single.php for specific category
function my_single_template($single_template)
{
    if (in_category('your_category')) {
        $file = get_template_directory() . '/single-your_category.php';
        if (file_exists($file)) {
            return $file;
        }
    }
    return $single_template;
}
add_filter('single_template', 'my_single_template');

// Disable tags
function unregister_tags()
{
    unregister_taxonomy_for_object_type('post_tag', 'post');
}
add_action('init', 'unregister_tags');

// Remove submenu items
function remove_submenus()
{
    global $submenu;
    unset($submenu['index.php'][10]); // Removes 'Updates'
    unset($submenu['themes.php'][5]); // Removes 'Themes'
    unset($submenu['options-general.php'][15]); // Removes 'Writing'
    unset($submenu['options-general.php'][20]); // Removes 'Reading'
}
add_action('admin_menu', 'remove_submenus');

// Add thumbnail size ('thumbnail_name', width, height, crop);
add_image_size('thumb_id', 800, 600, false);

// Hide content area
function remove_textarea()
{
    remove_post_type_support('post', 'editor');
    remove_post_type_support('post', 'tags');
}
add_action('admin_init', 'remove_textarea');

// Remove tags support from posts
function unregister_tags()
{
    unregister_taxonomy_for_object_type('post_tag', 'post');
}
add_action('init', 'unregister_tags');

Get the Current Url

<?php
global $wp;

$current_url = home_url(add_query_arg(array(), $wp->request));

Implement PJAX Pagination

See https://github.com/MoOx/pjax for more details.

const initPJAX = () => {
  const pjax = new Pjax({
    selectors: ['title', 'meta[name=description]', '.js-Pjax'],
    scrollRestoration: true,
    cacheBust: false
  });

  pjax._handleResponse = pjax.handleResponse;

  pjax.handleResponse = function (responseText, request, href, options) {
    if (request.responseText.match('<html')) {
      pjax._handleResponse(responseText, request, href, options);
      const regex = /<body class=(.*?)>/;
      const getBodyClass = responseText.match(regex);
      body.attr('class', getBodyClass[1]);
    }
  };

  document.addEventListener('pjax:success', whenDOMReady);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment