{"id":9809,"date":"2013-04-24T18:50:19","date_gmt":"2013-04-24T18:50:19","guid":{"rendered":"http:\/\/musilda.cz\/?p=184"},"modified":"2013-04-24T18:50:19","modified_gmt":"2013-04-24T18:50:19","slug":"prihlasovaci-formular-jako-widget-do-sidebaru","status":"publish","type":"post","link":"https:\/\/affinite.io\/cs\/prihlasovaci-formular-jako-widget-do-sidebaru\/","title":{"rendered":"P\u0159ihla\u0161ovac\u00ed formul\u00e1\u0159 jako widget do sidebaru"},"content":{"rendered":"
Pokud jste ji\u017e \u0159e\u0161ili, jak umo\u017enit u\u017eivatel\u016fm, jak se p\u0159ihl\u00e1sit p\u0159\u00edmo z hlavn\u00ed strany internetov\u00e9 str\u00e1nky, ur\u010dit\u011b jste pou\u017eili jeden z voln\u011b dostupn\u00fdch plugin\u016f. My si dnes uk\u00e1\u017eeme, jak vytvo\u0159it widget bez nutnosti instalovat plugin. Widget si budeme moci zobrazit v jak\u00e9mkoliv definovan\u00e9m sidebaru.<\/p>\n
Nejprve si ve slo\u017ece \u0161ablony, kterou budeme pou\u017e\u00edvat, vytvo\u0159\u00edme slo\u017eku widgets. N\u00e1\u0161 widget si pojmenujeme Login Form, co\u017e asi nen\u00ed origin\u00e1ln\u00ed, ale pro na\u0161e pot\u0159eby to sta\u010d\u00ed. Do slo\u017eky ulo\u017e\u00edme soubor widget-login-form.php, kter\u00fd je zat\u00edm pr\u00e1zd\u00fd. Nyn\u00ed ho za\u010dneme zapl\u0148ovat.<\/p>\n
Na za\u010d\u00e1tek souboru vlo\u017e\u00edme \u00fadaje, s popisem a n\u00e1zvem widgetu. Tato \u010d\u00e1st je d\u016fle\u017eit\u00e1, podle n\u00ed wordpress definuje widget:<\/p>\n
\/*-----------------------------------------------------------------------------------\n\n\tPlugin Name: Sidebar login form\n\tPlugin URI:\n\tDescription: A widget with login form\n\tVersion: 1.0\n\tAuthor:\n\tAuthor URI:\n\n-----------------------------------------------------------------------------------*\/<\/pre>\nN\u00e1sledn\u011b widget zaregistrujeme:<\/p>\n
\/\/ Register widget\nfunction vm_login_form_widgets() {\n\tregister_widget( 'vm_Login_Form_Widget' );\n}<\/pre>\nP\u0159id\u00e1me incializaci widgetu:<\/p>\n
\/\/ Add function to widgets_init that'll load our widget\nadd_action( 'widgets_init', 'vm_login_form_widgets' );<\/pre>\nV\u0161echny widgety vych\u00e1z\u00ed z t\u0159\u00eddy WP_Widget, kterou pomoc\u00ed nov\u00e9 t\u0159\u00eddy roz\u0161\u00ed\u0159\u00edme:<\/p>\n
\/\/ Widget class\nclass vm_login_form_widget extends WP_Widget {\n}<\/pre>\nP\u0159\u00edpravu m\u00e1me hotovou a nyn\u00ed si do t\u0159\u00eddy vytvo\u0159\u00edme funkce, kter\u00e9 definuj\u00ed vlastn\u00ed widget.
\nVytvo\u0159\u00edme funkci se stejn\u00fdm n\u00e1zvem, kter\u00fd jsme pou\u017eili p\u0159i registraci widgetu a kter\u00e1 definuje z\u00e1kladn\u00ed vlastnosti widetu:<\/p>\n
function vm_Login_Form_Widget() {\n\n\t\/\/ Widget settings\n\t$widget_ops = array(\n\t\t'classname' => 'vm_login_form_widget',\n\t\t'description' => __('A widget display login form in sidebar.', 'theme')\n\t);\n\n\t\/\/ Widget control settings\n\t$control_ops = array(\n\t\t'width' => 259,\n\t\t'height' => 200,\n\t\t'id_base' => 'vm_login_form_widget'\n\t);\n\n\t\/\/ Create the widget\n\t$this->WP_Widget( 'vm_login_form_widget', __('Login Form', 'theme'),\n $widget_ops, $control_ops );\n\n}<\/pre>\nUr\u010dit\u011b jste si v\u0161imli, \u017ee v\u0161echny texty, kter\u00e9 se budou zobrazovat, vkl\u00e1d\u00e1me pomoc\u00ed __(), co\u017e je funkce p\u0159ekladu, d\u00edky tomu umo\u017en\u00edte p\u0159\u00edpadn\u00fd p\u0159eklad \u0161ablony. Jak p\u0159ipravit \u0161ablonu pro p\u0159eklad bude t\u00e9ma jednoho z dal\u0161\u00edch \u010dl\u00e1nk\u016f, ale ji\u017e nyn\u00ed se budeme sna\u017eit d\u011blat widget p\u0159ipraven\u00fd na p\u0159eklad.
\nDal\u0161\u00ed funkc\u00ed bude samotn\u00fd widget:<\/p>\n
function widget( $args, $instance ) {\n\textract( $args );\n\n\t\/\/ Our variables from the widget settings\n\t$title = apply_filters('widget_title', $instance['title'] );\n\n\t\/\/ Before widget (defined by theme functions file)\n\techo $before_widget;\n\n\t\/\/ Display the widget title if one was input\n\tif ( $title )\n\techo $before_title . $title . $after_title;\n\n if ( is_user_logged_in() ) { global $current_user; get_currentuserinfo();\n echo '<\/pre>\n‚.__(‚Login as:‘,’theme‘).‘ ‚.$current_user->user_login.‘
\n‚.__(‚Logout‘,’theme‘).‘<\/a><\/p>\n';\n\n }else{\n\n echo'<\/pre>\n