<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Joseph Clarke</title>
    <description>The latest articles on DEV Community by Joseph Clarke (@jclarke).</description>
    <link>https://dev.to/jclarke</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F419369%2F3e2c3776-5e13-4683-85fd-813043f72815.jpg</url>
      <title>DEV Community: Joseph Clarke</title>
      <link>https://dev.to/jclarke</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jclarke"/>
    <language>en</language>
    <item>
      <title>Changing the Logo Link in WordPress is surprisingly difficult so I wrote my first WordPress plugin</title>
      <dc:creator>Joseph Clarke</dc:creator>
      <pubDate>Tue, 30 Jun 2020 18:26:18 +0000</pubDate>
      <link>https://dev.to/jclarke/changing-the-logo-link-in-wordpress-is-surprisingly-difficult-so-i-wrote-my-first-wordpress-plugin-46e7</link>
      <guid>https://dev.to/jclarke/changing-the-logo-link-in-wordpress-is-surprisingly-difficult-so-i-wrote-my-first-wordpress-plugin-46e7</guid>
      <description>&lt;p&gt;When setting up this blog, I wanted the TaskBill logo to take the visitor to our primary site when they clicked on it. I couldn’t find a way to change this anywhere in WordPress, and it turns out the only way to change this is to add some PHP code!&lt;/p&gt;

&lt;p&gt;I write PHP code all the time, it isn’t a big deal for me, however, it seems like a lot of work to do something simple and I would have to create a child theme just for this small change. Here is the code you need to add to your themes function.php&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;add_filter( 'get_custom_logo', 'change_logo_url' );
function change_logo_url() {
    $logo_id = get_theme_mod( 'custom_logo' );
    $html = sprintf( '&amp;lt;a href="%1$s" class="custom-logo-link" rel="home" itemprop="url"&amp;gt;%2$s&amp;lt;/a&amp;gt;',
            esc_url( "https://taskbill.io" ),
            wp_get_attachment_image( $custom_logo_id, 'full', false, array(
                'class'    =&amp;gt; 'custom-logo',
            ) )
        );
    return $html;   
} 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I searched to see if there was a plugin to manage this simple task instead, there was only one available and it was generating errors when I ran it. I also inspected the code and it changes the link using JavaScript so if Google crawls your page for example, it wouldn’t see this link.&lt;/p&gt;

&lt;p&gt;At this point I decided this was a fantastic opportunity to learn how to create a WordPress plugin that simply adds an option in Settings called Set Logo Link and changes the logo link.  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://taskbill.io/easylogolinkchange.zip"&gt;Download&lt;/a&gt; it here if you need to easily set your logo link and enjoy how easy it was!&lt;/p&gt;

&lt;p&gt;Here is the code for anyone that wants to take a peak:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
/**
 * @package Easy Logo Link Change
 * @version 1.0
 */
/*
Plugin Name: Easy Logo Link Change
Plugin URI: https://blog.taskbill.io/easylogolinkchange
Description: This is a simple plugin to change the URL of your logo in the top left hand corner.
Author: TaskBill.io
Version: 1.0
Author URI: http://taskbill.io
*/

// Change the Logo Link using the get_custom_logo function.
add_filter( 'get_custom_logo', 'change_logo_url' );
function change_logo_url() {
    $logo_id = get_theme_mod( 'custom_logo' );
    $html = sprintf( '&amp;lt;a href="%1$s" class="custom-logo-link" rel="home" itemprop="url"&amp;gt;%2$s&amp;lt;/a&amp;gt;',
            esc_url( get_option("easylogolinkchange_url") ),
            wp_get_attachment_image( $logo_id, 'full', false, array(
                'class'    =&amp;gt; 'custom-logo',
            ) )
        );
    return $html;   
} 

// Add an option to save the custom url
function easylogolinkchange_register_settings() {
   add_option( 'easylogolinkchange_url', 'https://awesomesite.com');
   register_setting( 'easylogolinkchangeurl_options_group', 'easylogolinkchange_url', 'easylogolinkchangeurl_callback' );
}
add_action( 'admin_init', 'easylogolinkchange_register_settings' );

// Add the option to the setting in WordPress
function easylogolinkchange_register_options_page() {
  add_options_page('Easy Logo URL Set', 'Set Logo Link', 'manage_options', 'easylogolinkchange', 'easylogolinkchangeurl_options_page');
}
add_action('admin_menu', 'easylogolinkchange_register_options_page');

function easylogolinkchangeurl_options_page(){
?&amp;gt;
  &amp;lt;div&amp;gt;
  &amp;lt;?php screen_icon(); ?&amp;gt;
  &amp;lt;h2&amp;gt;Easy Logo Link Change&amp;lt;/h2&amp;gt;
  &amp;lt;form method="post" action="options.php"&amp;gt;
  &amp;lt;?php settings_fields( 'easylogolinkchangeurl_options_group' ); ?&amp;gt;
  &amp;lt;h3&amp;gt;Set the url below you would like linked to your logo.&amp;lt;/h3&amp;gt;
  &amp;lt;table&amp;gt;
  &amp;lt;tr valign="top"&amp;gt;
  &amp;lt;th scope="row"&amp;gt;&amp;lt;label for="easylogolinkchange_url"&amp;gt;Logo URL&amp;lt;/label&amp;gt;&amp;lt;/th&amp;gt;
  &amp;lt;td&amp;gt;&amp;lt;input type="text" id="easylogolinkchange_url" name="easylogolinkchange_url" value="&amp;lt;?php echo get_option("easylogolinkchange_url"); ?&amp;gt;" /&amp;gt;&amp;lt;/td&amp;gt;
  &amp;lt;/tr&amp;gt;
  &amp;lt;/table&amp;gt;
  &amp;lt;?php  submit_button(); ?&amp;gt;
  &amp;lt;/form&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;?php
    }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



</description>
      <category>webdev</category>
      <category>wordpress</category>
      <category>wordpressplugin</category>
    </item>
    <item>
      <title>CableReady is Magic</title>
      <dc:creator>Joseph Clarke</dc:creator>
      <pubDate>Sun, 28 Jun 2020 21:03:00 +0000</pubDate>
      <link>https://dev.to/jclarke/cableready-is-magic-22i0</link>
      <guid>https://dev.to/jclarke/cableready-is-magic-22i0</guid>
      <description>&lt;p&gt;CableReady makes it possible to create a real-time updating dashboard with zero Javascript written by you. With CableReady you can have an ActiveJob render an updated partial and then tell CableReady to update the div with the new HTML.&lt;/p&gt;

&lt;p&gt;For our example, let’s say we have an application that executes a task every minute that makes a call to a Twitter API to search for tweets that contain a keyword. If there are new results found, we want to update the user’s dashboard that created the search.&lt;/p&gt;

&lt;p&gt;The first thing you need to do is add CableReady to your project using bundler and yarn:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bundle add cable_ready
yarn add cable_ready
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We only want the updates to go to the user that created the search. You can do that by creating a UserChannel and setting the stream_for name with the signed in users id.   &lt;/p&gt;

&lt;p&gt;Create the channel with the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bundle exec rails generate channel user
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Edit the auto generated user_channel and rename the stream_for to include the id of the current logged in user.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app/channels/user_channel.rb
class UserChannel &amp;lt; ApplicationCable::Channel 
  def subscribed 
     stream_for "user_channel_#{current\_user.id}"
  end
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Edit the user_channel.js JavaScript to include the CableReady magic.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app/javascript/channels/user_channel.js
import CableReady from 'cable_ready'
import consumer from './consumer'​consumer.subscriptions.create('UserChannel', { received (data) { 
  if(data.cableReady) CableReady.perform(data.operations) 
}
})
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In our dashboard view, simply render a partial and make sure you wrap it in a div tag.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app/views/dashboard.html.erb
&amp;lt;div id="dashboard_tweets"&amp;gt; 
  &amp;lt;%= render partial: "tweets", locals: {tweets: @tweets} %&amp;gt; &amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In our ActiveJob code, make sure you include the CableReady broadcaster . We then make a call to fetch the new tweets from our Twitter API.  &lt;/p&gt;

&lt;p&gt;Then the magic comes into play, we render the updated partial in our job and send it over the WebSocket using Cable Ready.   &lt;/p&gt;

&lt;p&gt;We do this by giving cable ready our user channel name we created above and include the user id of the person who owns the search. We tell it to replace the inner_html of the given selector of our dashboard_tweets div with the HTML partial we rendered in tweets_html.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app/jobs/fetch_tweets\_job.rb
class FetchTweetsJob &amp;lt; ApplicationJob 
  include CableReady::Broadcaster queue_as :default 

   def perform(search_id) 
     search = Search.find(search_id) tweets = 
     search.fetch_new_tweets 

     # Render the HTML Partial with the new data from tweets 
     tweets_html = ApplicationController.renderer.render( 
       partial "dashboard/tweets", locals" { tweets: tweets}) 

     # Tell CableReady what to update   
 cable_ready["user:user_channel_{search.user_id}"].inner_html( 
    selector: "#dashboard_tweets", 
    html: tweets_html ) 

# Broadcast the changes. cable_ready.broadcastend 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That’s it as soon as you sent the cable_ready.broadcast command the users dashboard will be updated automatically.  &lt;/p&gt;

&lt;p&gt;For more information on all of the awesome things you can do with CableReady, check out the documentation at &lt;a href="https://cableready.stimulusreflex.com/"&gt;https://cableready.stimulusreflex.com/&lt;/a&gt;.   &lt;/p&gt;

&lt;p&gt;If you need help using CableReady, feel free to comment below!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>rails</category>
      <category>ruby</category>
    </item>
  </channel>
</rss>
