Skip to content

Instantly share code, notes, and snippets.

View annalinneajohansson's full-sized avatar
🐱

Anna annalinneajohansson

🐱
  • Frontwalker Group AB
  • Sweden
  • 04:34 (UTC +02:00)
View GitHub Profile
@annalinneajohansson
annalinneajohansson / plugin-settings.php
Last active February 23, 2023 04:42
A base for a WordPress plugin settings page, using the Settings API #add_options_page #add_action #admin_init #register_setting #add_settings_section
<?php
# http://kovshenin.com/2012/the-wordpress-settings-api/
# http://codex.wordpress.org/Settings_API
add_action( 'admin_menu', 'my_admin_menu' );
function my_admin_menu() {
add_options_page( __('My Plugin Options', 'textdomain' ), __('My Plugin Options', 'textdomain' ), 'manage_options', 'my-plugin', 'my_options_page' );
}
add_action( 'admin_init', 'my_admin_init' );
@annalinneajohansson
annalinneajohansson / order_by_multiple_meta_values.php
Last active January 17, 2018 14:40
Solution to order posts by two different meta values (the same way ORDER val1, val2 would with SQL) http://wordpress.stackexchange.com/a/67391/5045
<?php
/*
* Solution to order first by date, then by start time (both are meta values)
* http://wordpress.stackexchange.com/a/67391/5045
* */
add_action( 'pre_get_posts', 'pre_get_posts_programpunkter' );
function pre_get_posts_programpunkter( $query ) {
if( !is_admin() && is_post_type_archive( 'programpunkt' ) && $query->is_main_query() ) {
@annalinneajohansson
annalinneajohansson / hip_hide_widget_titles.php
Created May 16, 2013 08:38
Hide widget titles by using an exclamation mark
<?php
/*
Plugin Name: Hide Widget Titles!
Plugin URI:
Description: Prevent widget titles from displaying frontend by prepending them with an exclamation mark (ie. !Widget Title)
Version: 1
Author: Hippies
Author URI: http://hippies.se/
**************************************************************************
@annalinneajohansson
annalinneajohansson / populate_children_hierarchically.php
Last active December 18, 2015 01:58
Populera $child_array rekursivt så att sidornas hierarkier bibehålls
<?php
function populate_children_hierarchically( $child ) {
$child_array = array( 'title' => $child->title, 'url' => $child->url );
if( !empty( $child->children ) ) {
foreach( $child->children as $granchild ) {
$child_array[] = populate_children_hierarchically( $granchild );
}
}
@annalinneajohansson
annalinneajohansson / schedule_event.php
Last active December 18, 2015 02:59
Schedule events in WordPress
<?php
/* SCHEDULE AN EVENT TO RUN EVERY HOUR */
/*
When you schedule an event in a plugin,
call wp_schedule_event on plugin activation otherwise you will end up with a lot of scheduled events.
*/
register_activation_hook(__FILE__, 'my_plugin_activation');
function my_plugin_activation() {
wp_schedule_event( time(), 'hourly', 'my_hourly_event_action');
}
@annalinneajohansson
annalinneajohansson / plugin-base.php
Created June 20, 2013 06:51
Base for WordPress plugin
<?php
/*
Plugin Name: PLUGIN NAME
Plugin URI:
Description: PLUGIN DESCRIPTION
Version: 1
Author: Hippies
Author URI: http://hippies.se/
**************************************************************************
<?php
/**
* Add "first" and "last" CSS classes to dynamic sidebar widgets. Also adds numeric index class for each widget (widget-1, widget-2, etc.)
*/
function hip_widget_first_last_classes( $params ) {
global $my_widget_num; // Global a counter array
$this_id = $params[0]['id']; // Get the id for the current sidebar we're processing
$arr_registered_widgets = wp_get_sidebars_widgets(); // Get an array of ALL registered widgets
@annalinneajohansson
annalinneajohansson / getUrlParameter.js
Created July 24, 2013 10:45
Get url parameters by name (javascript)
function getUrlParameter(a) {
a = a.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
a = RegExp("[\\?&]" + a + "=([^&#]*)").exec(location.search);
return null === a ? "" : decodeURIComponent(a[1].replace(/\+/g, " "))
}
@annalinneajohansson
annalinneajohansson / even_or_odd.php
Created July 26, 2013 05:46
odd / even iteration
<?php
$i = 0;
$even_or_odd = ( $i % 2 == 0 ) ? 'even' : 'odd';
++$i;
@annalinneajohansson
annalinneajohansson / ajax.js
Last active December 31, 2023 05:50
Basic AJAX function in WordPress
jQuery(document).ready(function($){
var ajaxurl = object.ajaxurl;
var data = {
action: 'my_action', // wp_ajax_my_action / wp_ajax_nopriv_my_action in ajax.php. Can be named anything.
foobar: 'some value', // translates into $_POST['foobar'] in PHP
};
$.post(ajaxurl, data, function(response) {
alert("Server returned this:" + response);
});
});