Seznam užitečných tipů, které usnadní práci a vývoj v redakčním systému wordpress:
Shortcode
Lze využít pro uživatelské vkládání nejrůznějšího obsahu přímo do postu, stránek.
Nejdříve si v souboru function.php vytvoříme a zaregistrujeme libovolnou funkci, kterou chceme aplikovat a vložit do článku.
function hello() {
return 'Hello, World!';
}
add_shortcode('nase_fce', 'hello'); |
function hello() {
return 'Hello, World!';
}
add_shortcode('nase_fce', 'hello');
Následně můžeme naši fci volat při tvorbě příspěvku:
Interakce s DB
Získání záznamů:
<!--?php $posts = $wpdb--->get_results("SELECT ID, post_title FROM wp_posts WHERE post_status = 'future'
AND post_type='post' ORDER BY post_date ASC LIMIT 0,4")
// Echo the title of the first scheduled post
echo $posts[0]->post_title;
?> |
<!--?php $posts = $wpdb--->get_results("SELECT ID, post_title FROM wp_posts WHERE post_status = 'future'
AND post_type='post' ORDER BY post_date ASC LIMIT 0,4")
// Echo the title of the first scheduled post
echo $posts[0]->post_title;
?>
Získání řádku:
<!--?php $posts = $wpdb--->get_row("SELECT ID, post_title FROM wp_posts WHERE post_status = 'publish'
AND post_type='post' ORDER BY comment_count DESC LIMIT 0,1")
// Echo the title of the most commented post
echo $posts->post_title;
?> |
<!--?php $posts = $wpdb--->get_row("SELECT ID, post_title FROM wp_posts WHERE post_status = 'publish'
AND post_type='post' ORDER BY comment_count DESC LIMIT 0,1")
// Echo the title of the most commented post
echo $posts->post_title;
?>
Získání sloupce:
<!--?php $posts = $wpdb--->get_col("SELECT ID FROM wp_posts WHERE post_status = 'publish'
AND post_type='post' ORDER BY comment_count DESC LIMIT 0,10")
// Echo the ID of the 4th most commented post
echo $posts[3]->ID;
?> |
<!--?php $posts = $wpdb--->get_col("SELECT ID FROM wp_posts WHERE post_status = 'publish'
AND post_type='post' ORDER BY comment_count DESC LIMIT 0,10")
// Echo the ID of the 4th most commented post
echo $posts[3]->ID;
?>
Získání konkrétní hodnoty:
<!--?php $email = $wpdb--->get_var("SELECT user_email FROM wp_users WHERE user_login = 'danielpataki' ")
// Echo the user's email address
echo $email;
?> |
<!--?php $email = $wpdb--->get_var("SELECT user_email FROM wp_users WHERE user_login = 'danielpataki' ")
// Echo the user's email address
echo $email;
?>
Vložení dat do DB:
$wpdb->insert( $table, $data, $format); |
$wpdb->insert( $table, $data, $format);
Vložení dat do DB ukázka:
<!--?php $wpdb--->insert($wpdb->usermeta, array("user_id" => 1, "meta_key" => "awesome_factor", "meta_value" => 10), array("%d", %s", "%d"));
// Equivalent to:
// INSERT INTO wp_usermeta (user_id, meta_key, meta_value) VALUES (1, "awesome_factor", 10);
?> |
<!--?php $wpdb--->insert($wpdb->usermeta, array("user_id" => 1, "meta_key" => "awesome_factor", "meta_value" => 10), array("%d", %s", "%d"));
// Equivalent to:
// INSERT INTO wp_usermeta (user_id, meta_key, meta_value) VALUES (1, "awesome_factor", 10);
?>
Update dat v DB
$wpdb->update( $table, $data, $where, $format = null, $where_format = null ); |
$wpdb->update( $table, $data, $where, $format = null, $where_format = null );
Update dat v DB ukázka:
$wpdb->update( $wpdb->posts, array("post_title" => "Modified Post Title"), array("ID" => 5), array("%s"), array("%d") ); |
$wpdb->update( $wpdb->posts, array("post_title" => "Modified Post Title"), array("ID" => 5), array("%s"), array("%d") );
Vložení vlastního loga na loginpage
add_action( 'login_head', 'ilc_custom_login');
function ilc_custom_login() {
echo '<style type="text/css">
h1 a { background-image:url('. get_stylesheet_directory_uri() . '/images/login-logo.png' . ') !important; margin-bottom: 10px; }
padding: 20px;}
</style>
<script type="text/javascript">window.onload = function(){document.getElementById("login").getElementsByTagName("a")[0].href = "'. home_url() . '";document.getElementById("login").getElementsByTagName("a")[0].title = "Go to site";}</script>';
} |
add_action( 'login_head', 'ilc_custom_login');
function ilc_custom_login() {
echo '<style type="text/css">
h1 a { background-image:url('. get_stylesheet_directory_uri() . '/images/login-logo.png' . ') !important; margin-bottom: 10px; }
padding: 20px;}
</style>
<script type="text/javascript">window.onload = function(){document.getElementById("login").getElementsByTagName("a")[0].href = "'. home_url() . '";document.getElementById("login").getElementsByTagName("a")[0].title = "Go to site";}</script>';
}
Změna počtu skov v excerpt funkci
add_filter('excerpt_length', 'ilc_excerpt_length');
function ilc_excerpt_length( $length ){
return 10;
} |
add_filter('excerpt_length', 'ilc_excerpt_length');
function ilc_excerpt_length( $length ){
return 10;
}